【问题标题】:ggplot, drawing lines across multiple facets (x axis is date)ggplot,在多个方面画线(x 轴是日期)
【发布时间】:2020-02-21 02:08:59
【问题描述】:

我的问题几乎与this question 相同,除了我在 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

【问题讨论】:

  • 您应该edit your question 并包含您尝试过的代码。
  • 还请包括您的部分数据框 - 选择您正在绘制的列并使用 dput(head(your_dataframe), 20) 并发布输出以共享前 20 行。

标签: ggplot2 grid facet gtable


【解决方案1】:

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

关键在于改变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

【讨论】:

    猜你喜欢
    • 2021-11-07
    • 2014-01-26
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    • 1970-01-01
    • 2019-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多