문제

I am using the R function segments and would like to know how I can draw "around" the segment (the contour), in say black.

plot(0)
segments(.9,.1,.8,.3, lwd=10, lend='square', col='pink')

Here I would get a black rectangle around the pink segment enter image description here

도움이 되었습니까?

해결책

You can draw it twice, first in black, large (lwd=12), then in pink, smaller (lwd=10).

plot(0)
segments(.9,.1,.8,.3, lwd=12, lend='square', col='black')
segments(.9,.1,.8,.3, lwd=10, lend='square', col='pink')

다른 팁

This is pretty messy, but I threw it together anyway.

draw.rect <- function(x1=0.9,y1=0.1,x2=0.8,y2=0.3,width=0.05){
  ang <- atan((y2-y1)/(x2-x1))
  xshift <- width*sin(ang)
  yshift <- width*cos(ang)
  polygon(x=c(x1,x2,x2-xshift,x1-xshift),y=c(y1,y2,y2+yshift,y1+yshift),col="pink")
}

It would allow you to use your same coordinates. You can adjust the size of the rectangle with the width argument. I think @VincentZoonekynd has a great idea with drawing the segment twice. This rough function does not center the rectangle on the coordinates provided, although you could pretty easily adjust it to do so.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top