在geom_segment中为细分添加边框

时间:2015-03-12 00:24:05

标签: r ggplot2

我试图弄清楚如何更改使用geom_segment的甘特图的边框颜色。

以下是一些数据:

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))

这是一个情节:

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

产生这个:

enter image description here

现在我该如何在每个细分周围添加红色边框?

geom_segment是这样做的,还是geom_rect会更合适? (由于分类的y轴,不确定它是否会起作用。)

1 个答案:

答案 0 :(得分:5)

您可以覆盖较小的&#39;在较大的&#39;上划分之一:

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()

overlaid segments