ggplot,在多个方面画线(x 轴是日期)

ggplot, drawing lines across multiple facets (x axis is date)

我的问题几乎与 相同,只是我在 x 轴上使用了日期。我已经尝试了链接问题中答案的代码。提供的示例适用于我,但我无法让它适用于我的数据集。我猜是因为日期?

(对不起,我无法对之前的问题链发表评论——我是新人,没有足够的积分发表评论)

示例代码如下:

library(ggplot2)
library(gtable)
library(grid)

data<-data.frame(Date=rep(seq(as.Date("2018-09-22","%Y-%m-%d"),
                              as.Date("2019-06-19","%Y-%m-%d"),
                              by=30),9),
                 Station=c(rep("A",30),rep("B",30),rep("C",30)),
                 Description=rep(c(rep("Var1",10),rep("Var2",10),
                                   rep("Var3",10)),3),
Data=c(seq(1,10,by=1),seq(500,800,length.out=10),seq(30,90,length.out=10),                   seq(5,19,length.out=10),seq(450,1080,length.out=10),seq(20,60,length.out=10),                  seq(2,15,length.out=10),seq(600,750,length.out=10),seq(80,25,length.out=10)))

plot<-ggplot(data,aes(x=Date,y=Data,color=as.factor(Station)))+
  geom_line(size=1)+
  facet_grid(Description~.,scales="free_y",switch="y")+
  xlab("")+
  ylab("")+
  theme(panel.background=element_blank(),
        panel.grid.major.y=element_line(color="grey80",
                                        size=0.25),
        panel.grid.major.x=element_blank(),
        axis.line=element_line(color="black"),
        strip.placement="outside",
        strip.background=element_blank(),
        legend.position="top",
        legend.key=element_blank(),
        legend.title=element_blank())
plot
plot.b<-ggplot_build(plot)
plot.g<-ggplot_gtable(plot.b)
data2npc <- function(x, panel = 1L, axis = "x") {
  range <- plot.b$layout$panel_params[[panel]][[paste0(axis,".range")]]
  scales::rescale(c(range, x), c(0,1))[-c(1,2)]
}
start <- sapply(as.Date("2018-10-10"),"%Y-%m-%d"), data2npc, panel=1, axis="x")
plot.g <- gtable_add_grob(plot.g, segmentsGrob(x0=start, x1=start, y0=0, y1=1, gp=gpar(lty=2)), t=7, b=9,l=5)

grid.newpage()
grid.draw(plot.g)

resulting plot

我已经找到了自己的答案!

关键在于改变 gtable_add_grob 中的 t、b 和 l:

plot.g <- gtable_add_grob(plot.g, segmentsGrob(x0=start, x1=start, y0=0, y1=1, gp=gpar(lty=2)), t=7, b=13,l=7)

不过,在我看来,确定 t、b 和 l 的正确值需要反复试验。

新代码:

library(ggplot2)
library(gtable)
library(grid)

data<-data.frame(Date=rep(seq(as.Date("2018-09-22","%Y-%m-%d"),
                              as.Date("2019-06-19","%Y-%m-%d"),
                              by=30),9),
                 Station=c(rep("A",30),rep("B",30),rep("C",30)),
                 Description=rep(c(rep("Var1",10),rep("Var2",10),
                                   rep("Var3",10)),3),
Data=c(seq(1,10,by=1),seq(500,800,length.out=10),seq(30,90,length.out=10),                seq(5,19,length.out=10),seq(450,1080,length.out=10),seq(20,60,length.out=10),                     seq(2,15,length.out=10),seq(600,750,length.out=10),seq(80,25,length.out=10)))

plot<-ggplot(data,aes(x=Date,y=Data,color=as.factor(Station)))+
  geom_line(size=1)+
  facet_grid(Description~.,scales="free_y",switch="y")+
  xlab("")+
  ylab("")+
  theme(panel.background=element_blank(),
        panel.grid.major.y=element_line(color="grey80",
                                        size=0.25),
        panel.grid.major.x=element_blank(),
        axis.line=element_line(color="black"),
        strip.placement="outside",
        strip.background=element_blank(),
        legend.position="top",
        legend.key=element_blank(),
        legend.title=element_blank())
plot
plot.b<-ggplot_build(plot)
plot.g<-ggplot_gtable(plot.b)
data2npc <- function(x, panel = 1L, axis = "x") {
  range <- plot.b$layout$panel_params[[panel]][[paste0(axis,".range")]]
  scales::rescale(c(range, x), c(0,1))[-c(1,2)]
}
start <- sapply(as.Date("2018-10-10","%Y-%m-%d"), data2npc, panel=1, axis="x")
plot.g <- gtable_add_grob(plot.g, segmentsGrob(x0=start, x1=start, y0=0, y1=1, gp=gpar(lty=2)), t=7, b=13,l=7)

grid.newpage()
grid.draw(plot.g)

和new resulting plot