向 ggplot 条形图添加垂直线

B. *_*vis 4 r ggplot2

ggplot我正在尝试向显示每月计数数据的垂直线添加。我的 x 轴是月份作为因子,但我的垂直线代表儒略日。

例如,使用这些数据:

dat <- structure(list(Month = structure(1:12, .Label = c("Jan", "Feb", 
"Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", 
"Dec"), class = c("ordered", "factor")), Data = c(1, 1, 2, 2, 
6, 11, 19, 23, 19, 13, 5, 1)), .Names = c("Month", "Data"), class = "data.frame", row.names = c(NA, 
-12L))
Run Code Online (Sandbox Code Playgroud)

我可以制作以下条形图

ggplot(dat)+ geom_bar(aes(x=  Month, y = Data), stat="identity")
Run Code Online (Sandbox Code Playgroud)

然后,如何使用儒略日 68 和 252 的 x 截距添加两条垂直线geom_vline

我不知道如何绘制参考每月(因子)x 轴数据上的连续刻度的线。

raf*_*ira 5

当您的x轴是 a时factor,则将geom_vline使用每个值出现的顺序作为其截距。

然后,您只需确定哪个分数与您要查找的确切日期相对应。在此示例中,我使用了两个说明性分数。

ggplot(dat)+ geom_bar(aes(x=  Month, y = Data), stat="identity") + 
geom_vline(xintercept = 5.2) +
  geom_vline(xintercept = 8.7) 
Run Code Online (Sandbox Code Playgroud)