It is not possible to get lines of short length to display in the plot

I'm having trouble getting segments of short lengths to appear in my plot.

Assuming the following sample data:

x=c(11,22,33,44,55) y=c(15,23,33,45,57) z=strptime(20120101:20120105,'%Y%m%d') 

If I had to create segments from this data, my segment for the third record does not appear if I want the line to end or the end of the line. It appears if I allow the ends of my string lend=0 .

 plot(z,x,type='n') segments(as.numeric(z),x,as.numeric(z),y,lwd=5,lend=2) 

If I try this:

 segments(as.numeric(z),x,as.numeric(z),y,lwd=5,lend=0) 

He shows a circle at 33. Is there a way to get at least a flat line that appears at 33 (hopefully in the base)?

I would use my actual data, which also does this when the range is small, for example, from 33.0005 to 33.0010, but this data is huge, and I was hoping that the solution, when they are the same, would also solve for small ranges.

ETA: If lwd=15 , the circle looks even more ridiculous.

Perhaps the segments are not suitable for this?

This is for a candlestick, so these numbers will be displayed both open and closed. I also have high and low numbers that go beyond this range and are drawn using lwd=1 in these segments.

-one
source share
4 answers

Basic graphics provide a rectangle. And in fact, he does what you want. Using your definitions above.

 xdiff <- max(as.numeric(z)) - min(as.numeric(z)) segwidth <- xdiff/50 plot(z,x,type='n') rect(z-segwidth/2, x, z+segwidth/2, y, col="black") 
+3
source

As @Joran points out, this could very well be the β€œright” behavior.

But a workaround is to simply add an arbitrary small number to the values. This value should be small enough not to β€œdistort” the data, but large enough to appear in your plot, given the resolution of your plot device.

 delta <- pmax(0.2, y - x) plot(z,x,type='n') segments(as.numeric(z),x ,y1 = y + delta, lwd=10, lend=1) 

enter image description here


PS. I advise against this. You have been warned.

+4
source

Given the changes made to your question, I suspect that the way to this is to lay dots indicating your open and closed, and a segment to indicate the range.

Thus, if your open and closed points are identical (or close), you will get the symbol at the correct point.

 x <- strptime(20120101:20120105,'%Y%m%d') y1 <- c(11,22,33,44,55) y2 <- c(15,23,33,45,57) r <- range(c(y1, y2)) plot(c(x, x), c(y1, y2), type="n", xlab="Date", ylab="y") points(x, y1, pch=18) points(x, y2, pch=18) segments(as.numeric(x), y0=y1, y1=y2) 

enter image description here

+2
source

There is something a little strange about the "square" line

 library(grid) epsilon <- 1e-4 grid.newpage() grid.points(x=c(0.5-epsilon,0.5+epsilon), y=c(0.5,0.5), pch="+", gp=gpar(cex=2), def="npc") grid.segments(0.5-epsilon, 0.5, 0.5+epsilon, 0.5, gp=gpar(lineend="square",lwd=50, alpha=0.2)) grid.segments(0.5-epsilon, 0.5, 0.5+epsilon, 0.5, gp=gpar(lineend="round",lwd=50, alpha=0.2)) grid.segments(0.5-epsilon, 0.5, 0.5+epsilon, 0.5, gp=gpar(lineend="butt",lwd=50, alpha=0.2)) 

behavior has a jump at epsilon = 0,

enter image description here for epsilon = 1e-4 vs

enter image description here for epsilon = 0

As a workaround, I draw rectangles instead of lines; they always have at least one line width.

 grid.newpage() grid.rect(x=0.5, y=0.5, width=0.01, height=0, gp=gpar(fill="black", col="red", lwd=10, linejoin="mitre")) 
+2
source

All Articles