且构网

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

如何在段的开头将标签添加到geom_segment?

更新时间:2022-01-04 07:58:07

ggplot() +
  geom_segment(data=cambodia, aes(x=StartDate, xend=EndDate, y=Period, yend=Period, color=Color), 
               linetype=1, size=2) +
  geom_label(data=cambodia, aes(x=StartDate, y=Period,  label = Period),
             nudge_x = c(-300, -200, -200, -100)) +
  scale_colour_brewer(palette = "Pastel1")+
  xlab("Date")+
  ylab("")+
  theme_bw() + 
  theme(legend.position="none") +
  theme(aspect.ratio = .2) +
  theme(panel.grid.minor = element_blank(), panel.grid.major =   element_blank(),
        axis.line.y = element_blank(), axis.text.y = element_blank(),
        axis.ticks.y = element_blank())

您需要使用 element_blank()删除y轴元素,然后在 geom_label 中使用 nudge_x 参数适当地偏移标签.

You need to use element_blank() to remove the y axis elements and then use nudge_x argument in geom_label to offset the labels appropriately.