ggplot2 - 在x轴上添加日期的水平线到刻面图

时间:2022-11-30 09:45:37

I'm trying to add a zero line to a faceted plot in ggplot2 with dates on the x-axis. The problem is that I'm also adding polygons to represent certain time spans, so I have to pass separate data.frames to separate geoms, which yields some difficulties.

我正在尝试在ggplot2中的刻面图上添加零线,并在x轴上添加日期。问题是我也在添加多边形以表示某些时间跨度,因此我必须将单独的data.frames传递给单独的geoms,这会产生一些困难。

Here is an example with a continuous x-axis:

以下是连续x轴的示例:

ggplot()+
  geom_rect(data=data.frame(from=c(1,3),to=c(2,4)),aes(xmin=from,xmax=to,ymin=-Inf,ymax=Inf),fill="red",alpha=0.1)+
  geom_point(data=data.frame(x=c(1,2,3,4,5,6),y=c(6,7,8,9,10,11),type=rep(letters[1:2],each=3)),aes(x=x,y=y))+
  facet_grid(type~.)

If I try to add a horizontal line with geom_hline I get an error: Error in if (empty(data)) { : missing value where TRUE/FALSE needed, which I take is because geom_vline needs to inherit info provided in the base ggplot line. As noted above, however, I must provide separate data.frames to create both points and shaded polygons.

如果我尝试使用geom_hline添加水平线,我会收到错误:if(空(数据)){:缺少值需要TRUE / FALSE,我采取的是因为geom_vline需要继承基本ggplot行中提供的信息。但是,如上所述,我必须提供单独的data.frames来创建点和阴影多边形。

This can be worked around if the x-axis is continuous by using geom_line and setting the values to Inf:

如果x轴是连续的,可以使用geom_line并将值设置为Inf来解决这个问题:

ggplot()+
  geom_line(data=data.frame(x=c(-Inf,Inf),y=0),aes(x=x,y=y),col="grey50",lwd=1)+
  geom_rect(data=data.frame(from=c(1,3),to=c(2,4)),aes(xmin=from,xmax=to,ymin=-Inf,ymax=Inf),fill="red",alpha=0.1)+
  geom_point(data=data.frame(x=c(1,2,3,4,5,6),y=c(6,7,8,9,10,11),type=rep(letters[1:2],each=3)),aes(x=x,y=y))+
  facet_grid(type~.)

But if I switch the x-axis to dates, then I can't add a horizontal line using geom_hline (for the same reasons as above):

但是如果我将x轴切换到日期,那么我就无法使用geom_hline添加水平线(出于与上述相同的原因):

dates=c("2001-01-1","2002-01-01","2003-01-01","2004-01-01","2005-01-01","2006-01-01")

ggplot()+
  geom_hline(aes(yintercept=0))+
  geom_rect(data=data.frame(from=c(as.Date("2001-01-1"),as.Date("2003-01-01")),
                            to=c(as.Date("2002-01-1"),as.Date("2004-01-01"))),
            aes(xmin=from,xmax=to,ymin=-Inf,ymax=Inf),fill="red",alpha=0.1)+
  geom_point(data=data.frame(x=as.Date(dates),y=c(6,7,8,9,10,11),type=rep(letters[1:2],each=3)),aes(x=x,y=y))+
  facet_grid(type~.)

Similarly, using geom_line as above produces an error: Error: Discrete value supplied to continuous scale because the x-axis is no longer continuous.

同样,如上所述使用geom_line会产生错误:错误:提供给连续比例的离散值,因为x轴不再是连续的。

I can specify the endpoints of geom_line as dates:

我可以将geom_line的端点指定为日期:

ggplot()+
  geom_line(data=data.frame(x=c(as.Date("2001-01-01"),as.Date("2006-01-01")),y=0),aes(x=x,y=y),col="grey50",lwd=1)+
  geom_rect(data=data.frame(from=c(as.Date("2001-01-1"),as.Date("2003-01-01")),
                            to=c(as.Date("2002-01-1"),as.Date("2004-01-01"))),
            aes(xmin=from,xmax=to,ymin=-Inf,ymax=Inf),fill="red",alpha=0.1)+
  geom_point(data=data.frame(x=as.Date(dates),y=c(6,7,8,9,10,11),type=rep(letters[1:2],each=3)),aes(x=x,y=y))+
  facet_grid(type~.)

But now the line does not extend the length of the plot!

但现在这条线并没有延长情节的长度!

How can I reproduce a geom_vline-like output using something that will work with dates on the x-axis and facets?

如何使用可以在x轴和刻面上使用日期的东西来重现类似geom_vline的输出?

1 个解决方案

#1


8  

Your problem is in fact quite different, and also very easy to fix.

你的问题实际上是完全不同的,也很容易修复。

Since the value of yintercept in your hline is not related to your data, but a user-specified value, this should not form part of the aes().

由于hline中的yintercept值与您的数据无关,而与用户指定的值无关,因此不应构成aes()的一部分。

Instead, the specification is simply:

相反,规范很简单:

geom_hline(yintercept=0) +

Try this:

尝试这个:

dates=c("2001-01-1","2002-01-01","2003-01-01","2004-01-01","2005-01-01","2006-01-01")

ggplot()+
  geom_rect(data=data.frame(from=c(as.Date("2001-01-1"),as.Date("2003-01-01")),
                            to=c(as.Date("2002-01-1"),as.Date("2004-01-01"))),
            aes(xmin=from,xmax=to,ymin=-Inf,ymax=Inf),fill="red",alpha=0.1) +
  geom_point(data=data.frame(x=as.Date(dates),y=c(6,7,8,9,10,11),type=rep(letters[1:2],each=3)),
             aes(x=x,y=y)) +
  geom_hline(yintercept=0) +
  facet_grid(type~.)

ggplot2  - 在x轴上添加日期的水平线到刻面图

#1


8  

Your problem is in fact quite different, and also very easy to fix.

你的问题实际上是完全不同的,也很容易修复。

Since the value of yintercept in your hline is not related to your data, but a user-specified value, this should not form part of the aes().

由于hline中的yintercept值与您的数据无关,而与用户指定的值无关,因此不应构成aes()的一部分。

Instead, the specification is simply:

相反,规范很简单:

geom_hline(yintercept=0) +

Try this:

尝试这个:

dates=c("2001-01-1","2002-01-01","2003-01-01","2004-01-01","2005-01-01","2006-01-01")

ggplot()+
  geom_rect(data=data.frame(from=c(as.Date("2001-01-1"),as.Date("2003-01-01")),
                            to=c(as.Date("2002-01-1"),as.Date("2004-01-01"))),
            aes(xmin=from,xmax=to,ymin=-Inf,ymax=Inf),fill="red",alpha=0.1) +
  geom_point(data=data.frame(x=as.Date(dates),y=c(6,7,8,9,10,11),type=rep(letters[1:2],each=3)),
             aes(x=x,y=y)) +
  geom_hline(yintercept=0) +
  facet_grid(type~.)

ggplot2  - 在x轴上添加日期的水平线到刻面图