首页 > 解决方案 > 有没有办法在我的图表上放置一条带有标签的垂直线?

问题描述

我试图在我的图表上的特定点放置垂直线。我正在使用ggplot2。我在互联网上环顾四周,没有任何效果。

气候变化新闻指标(标准化和非季节性)

# collect data into dataframes for autoplot
News           = cbind(CHNEG ,Google , WSJ )
NewsG_WSJ      = cbind(Google ,WSJ )
NewsG_CHNEG    = cbind(Google ,CHNEG )
NewsCHNEG_WSJ  = cbind(CHNEG ,WSJ )
NewsAll        = cbind(Google , WSJ ,CHNEG  )

# plot
autoplot(News)  #plot data series in individual panels 
autoplot(News          , facets = FALSE)   #plot together
autoplot(NewsCHNEG_WSJ , facets = FALSE)   #plot together
autoplot(NewsG_WSJ     , facets = FALSE)   #plot together
autoplot(NewsG_CHNEG   , facets = FALSE)   #plot together

数据采用时间序列,日期格式为 200806、200807 等。如果您需要更多信息来提供答案,请礼貌地向我询问更多信息。:) 谢谢!

标签: rdateggplot2time-series

解决方案


您可以使用geom_vline()添加垂直线和geom_text()为线添加标签。例如,

geom_vline(xintercept=(location), colour="grey") +
            #before line
  geom_text(aes(x=(location), label="\n (text here)", y=(location)), 
            colour="steelblue", angle=90, text=element_text(size=10)) +
            #after line
  geom_text(aes(x=(location), label="(text here)\n", y=(location)), 
            colour="darkred", angle=90, text=element_text(size=10))

这是 Iris 数据的示例 例子


推荐阅读