r语言 - ggplot 时间序列中的主要网格线



我正在尝试制作一些交货日期的图表。 我已经得到了我想要演示的大部分内容,但我想每年在图表中添加一条网格线。 我正在使用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")

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

谢谢

我已经设法使用geom_vline()破解了一个解决方案

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

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

谢谢

H

最新更新