ggplot时间序列中的主要网格线

时间:2015-01-14 09:15:22

标签: r ggplot2 time-series gridlines

我试图制作一些交付日期的图表。我已经掌握了我想展示的大部分内容,但我想在每年的图表中添加一个网格线。我正在使用ggplot来创建图表。这是代码:

structure(list(Task = c("Haptoglobin", "Transferrin", "A", "B", 
"C", "D", "E", "F", "G", "Haptoglobin", "Transferrin", "A", "B", 
"C", "D", "E", "F", "G"), Approach = c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), Delivery.Date = c("May-16", 
"Sep-16", "Mar-17", "Sep-17", "Sep-17", "Nov-17", "Apr-18", "Apr-18", 
"May-18", "May-16", "Sep-16", "Jan-17", "Jan-17", "Mar-17", "Aug-17", 
"Aug-17", "Nov-17", "Feb-18"), Date = c("2016-05-16", "2016-05-16", 
"2017-03-14", "2017-09-26", "2017-09-26", "2017-11-21", "2018-04-10", 
"2018-04-10", "2018-05-08", "2016-05-16", "2016-09-05", "2017-01-17", 
"2017-01-17", "2017-03-04", "2017-08-01", "2017-08-01", "2017-11-21", 
"2018-02-06"), date = structure(c(16937, 16937, 17239, 17435, 
17435, 17491, 17631, 17631, 17659, 16937, 17049, 17183, 17183, 
17229, 17379, 17379, 17491, 17568), class = "Date"), taskID = c(9, 
8, 7, 6, 5, 4, 3, 2, 1, 9, 8, 7, 6, 5, 4, 3, 2, 1)), .Names = c("Task", 
"Approach", "Delivery.Date", "Date", "date", "taskID"), row.names = c(NA, 
-18L), class = "data.frame")

approach_labller <- function(key, val){
return(approach_key[val])
}

approach_key <- list('1'="Approach 1", '2'="Approach 2")

ggplot(ppps, aes(date, taskID)) + geom_point(colour="blue", size=4, shape=15) + scale_x_date(labels=date_format("%b-%y"), breaks=date_breaks('month')) + facet_grid(Approach ~ ., labeller=approach_labller) + scale_y_discrete(limits=c(9,8,7,6,5,4,3,2,1), labels=t) + theme_bw(panel.grid.major.x(colour="black", size=2)) + labs(y="Target", x="Date")

所以我想要实现的是每年1月/年的主要X网格线。

由于

1 个答案:

答案 0 :(得分:1)

我已设法使用geom_vline()

破解解决方案
geom_vline(as.numeric(as.date('1 jan 17', format="%d %b %y"), colour="grey90", size=0.7)

但我想知道是否有更优雅的解决方案......

由于

ħ