且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将边框添加到geom_segment中的线段

更新时间:2023-12-03 12:32:46

'large'one:

  ggplot()+ 
geom_segment(data = df,aes(x = start_int,xend = end_int,y = ids,yend = ids),size = 11,color =red)+
geom_segment(data = df,aes(x =(start_int + 0.1),xend =(end_int-0.1) ,y = ids,yend = ids),size = 10)+
theme_bw()


I'm trying to figure out how to change the border colour for a gantt chart that uses geom_segment.

Here's some data:

df <- structure(list(start_int = c(0, 5, 0, 0, 9, 10, 12, 10, 14, 18, 
22, 24, 27, 29, 26), end_int = c(5, 6, 8, 9, 10, 12, 14, 15, 
15, 20, 23, 27, 30, 31, 32), ids = c("C", "C", "A", "C", "B", 
"B", "C", "A", "B", "C", "B", "B", "B", "B", "C")), .Names = c("start_int", 
"end_int", "ids"), class = c("data.frame"), row.names = c(10L, 11L, 1L, 14L, 3L, 6L, 12L, 2L, 7L, 13L, 4L, 8L, 9L, 5L, 15L))

And here's a plot:

ggplot(df, aes(x=start_int, xend = end_int, y = ids, yend = ids)) + 
  geom_segment(size = 10) +
  theme_bw()

Which produces this:

Now how do I go about adding a red border around each of those segments?

Is geom_segment the way to do it, or would geom_rect be more appropriate? (not sure it would work due to the categorical y-axis).

You can overlay a 'smaller' segment on a 'larger' one:

ggplot() + 
geom_segment(data=df, aes(x=start_int, xend=end_int, y=ids, yend=ids), size=11, colour="red") + 
geom_segment(data=df, aes(x=(start_int+0.1), xend=(end_int-0.1), y=ids, yend=ids), size=10) +
theme_bw()