简体   繁体   English

R ggplot2具有不同时间日期格式的绘图列表-这是一个错误吗?

[英]R ggplot2 list of plots with different timedate format - is that a bug?

I am trying to produce a list of ggplot2 graphs with different timedate format labels on the x axe. 我正在尝试在x轴上生成具有不同timedate格式标签的ggplot2图的列表。 Yet the format argument is not taken into account as expected by ggplot. 但是,ggplot并未考虑format参数。 Here is the minimal code that can reproduce the issue. 这是可以重现此问题的最少代码。

x<-as.POSIXct(strptime(c("2013-01-01 00:00:00","2013-01-02 00:00:00"),format="%Y-%m-%d %H:%M:%S"))
y1<-c(0,1)
y2<-c(0,-1)
df<-data.frame(x=x,y1=y1,y2=y2)
f<-function(a,b){ggplot(df,aes_string(x="x",y=b))+geom_line()+ scale_x_datetime(labels= date_format(a))}
r<-mapply(f,c("%b-%d","%H:%M"),c("y1","y2"),SIMPLIFY=FALSE)

r[2] gives the expected plot (sorry I can not post pictures) but for r[1] the axes format is not correct (whereas the data to plot is correctly taken into account). r[2]给出了预期的绘图(对不起,我无法发布图片),但是对于r[1] ,轴格式不正确(而正确考虑了绘图数据)。

any suggestions? 有什么建议么?

Between ggplot environments and mapply and promise evaluation, a is not being evaluated appropriately, you can get around this by force ing it within the function ggplot环境与mapply和promise评估之间,没有正确评估a ,您可以通过在函数中force将其解决

eg 例如

f<-function(a,b){
  a <- force(a)
  ggplot(df,aes_string(x="x",y=b))+geom_line()+ scale_x_datetime(labels= date_format(a))
}
r <- mapply(f,c("%b-%d","%H:%M"),c("y1","y2"), SIMPLIFY = FALSE)

r[1]

在此处输入图片说明

r[2]

在此处输入图片说明

Explain a lazy evaluation quirk explains the lazy evalation in more detail. 解释一个懒惰的评估怪癖更详细地解释了懒惰的评估

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM