簡體   English   中英

向ggplot條形圖添加垂直線

[英]adding vertical line to ggplot bar plot

我正在嘗試向顯示每月計數數據的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))

我可以做下面的條形圖

ggplot(dat)+ geom_bar(aes(x=  Month, y = Data), stat="identity")

然后,如何使用geom_vline在朱利安第68 geom_vline 252天的x截距處添加兩條垂直線?

我不確定如何在每月(因子)x軸數據上繪制參考連續刻度的線。

如果您的x軸是一個factor ,則geom_vline將使用每個值的出現順序作為其截距。

然后,您只需要確定與您要查找的確切日期相對應的分數即可。 在此示例中,我使用了兩個示例性的分數。

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

數據以每月一個單位的形式繪制,因此您需要將行添加為年份的一部分(以月為單位),並為條形尺寸添加一些偏移量:

ggplot(dat) +
  geom_bar(aes(x = Month, y = Data), stat = "identity") + 
  geom_vline(xintercept = 12 * (c(68, 252) / 365) + 0.5) 

通過查看第1天和第365天的行來進行檢查:

ggplot(dat) +
  geom_bar(aes(x = Month, y = Data), stat = "identity") + 
  geom_vline(xintercept = 12 * (c(1, 365) / 365) + 0.5) 

每隔一天將在圖表上進行線性插值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM