【问题标题】:R ggplot2 list of plots with different timedate format - is that a bug?R ggplot2 具有不同时间日期格式的绘图列表 - 这是一个错误吗?
【发布时间】:2013-04-08 09:46:44
【问题描述】:

我正在尝试在 x 轴上生成具有不同时间日期格式标签的 ggplot2 图列表。然而,格式参数并未像 ggplot 预期的那样被考虑在内。 这是可以重现该问题的最少代码。

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] 给出了预期的情节(对不起,我无法发布图片),但对于 r[1],坐标轴格式不正确(而要绘制的数据被正确考虑在内)。

有什么建议吗?

【问题讨论】:

    标签: r list ggplot2 format


    【解决方案1】:

    在ggplot 环境和mapply 和promise 评估之间,a 没有得到适当的评估,你可以通过在函数中forceing 来解决这个问题

    例如

    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 更详细地解释了惰性求值。

    【讨论】:

    • 行得通!多谢!您认为这应该被视为 ggplot 错误吗?
    • @user2147028 -- 这不是错误,更多的是懒惰评估的怪癖以及 mapply 和 lapply 的工作方式
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 2016-07-31
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 2021-01-30
    • 1970-01-01
    相关资源
    最近更新 更多