【问题标题】:Merge data.frames for grouped boxplot r合并分组箱线图r的data.frames
【发布时间】:2015-12-18 12:27:18
【问题描述】:

我有两个数据框 z(100 万次观察)和 b(500k 次观察)。

 z= Tracer time treatment
        15 0 S
        20 0 S
        25 0 X
        04 0 X
        55 15 S
        16 15 S
        15 15 X
        20 15 X

  b= Tracer time treatment
            2 0 S
            35 0 S
            10 0 X
            04 0 X
            20 15 S
            11 15 S
            12 15 X
            25 15 X

我想创建分组箱线图,使用时间作为因素,处理作为颜色。本质上,我需要将它们绑定在一起,然后区分它们,但不确定如何。我尝试的一种方法是使用:

zz<-factor(rep("Z", nrow(z))
bb<-factor(rep("B",nrow(b))
dumB<-merge(z,zz) #this won't work because it says it's too big
dumB<-merge(b,zz)
total<-rbind(dumB,dumZ)

但是 z 和 zz 合并不起作用,因为它说它的大小是 10G(这不可能)

最后的情节可能类似于这个例子:Boxplot with two levels and multiple data.frames

有什么想法吗?

干杯,

编辑:添加箱线图

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    我会这样处理它:

    # create a list of your data.frames
    l <- list(z,b)
    # assign names to the dataframes in the list
    names(l) <- c("z","b")
    
    # bind the dataframes together with rbindlist from data.table
    # the id parameter will create a variable with the names of the dataframes
    # you could also use 'bind_rows(l, .id="id")' from 'dplyr' for this
    library(data.table)
    zb <- rbindlist(l, id="id")
    
    # create the plot
    ggplot(zb, aes(x=factor(time), y=Tracer, color=treatment)) +
      geom_boxplot() +
      facet_wrap(~id) +
      theme_bw()
    

    给出:

    创建情节的其他替代方法:

    # facet by 'time'
    ggplot(zb, aes(x=id, y=Tracer, color=treatment)) + 
      geom_boxplot() + 
      facet_wrap(~time) + 
      theme_bw()
    
    # facet by 'time' & color by 'id' instead of 'treatment'
    ggplot(zb, aes(x=treatment, y=Tracer, color=id)) + 
      geom_boxplot() + 
      facet_wrap(~time) + 
      theme_bw()
    

    针对您的最后一条评论:要将所有内容集中在一个情节中,您可以使用interaction 来区分不同的分组,如下所示:

    ggplot(zb, aes(x=treatment, y=Tracer, color=interaction(id, time))) + 
      geom_boxplot(width = 0.7, position = position_dodge(width = 0.7)) + 
      theme_bw()
    

    给出:

    【讨论】:

    • 非常好,非常感谢!看起来很棒!是否可以按时间而不是按 z/b 刻面?
    • 我刚刚意识到我实际上是在同一张图上寻找箱线图,类似于本页上的最后一个图:plot.ly/r/box-plots 其中cut 是时间,价格是Tracer,color=treatment。这可能吗,因为我的图表在刻面时看起来没有可比性。 :-)
    • 非常感谢,谢谢,现在看起来很漂亮。我会玩弄我认为的颜色和宽度,让它看起来更明显。我已将交互转换为 id,现在进行治疗 :-) 非常感谢您的帮助
    • @HCAI 是的,请参阅更新。你必须在geom_boxplot 中使用width 参数来看看你最喜欢什么。
    【解决方案2】:

    关键是您不需要执行merge,这在大型表上的计算量很大。而是为每个数据帧分配一个新变量和值(下面我的代码中的源 c(b,z)),然后分配给rbind。然后它变得直截了当,我的解决方案与 @Jaap 的解决方案非常相似,只是有不同的方面。

    library(ggplot2)
    #Create some mock data
    t<-seq(1,55,by=2)
    z<-data.frame(tracer=sample(t,size = 10,replace = T), time=c(0,15), treatment=c("S","X"))
    b<-data.frame(tracer=sample(t,size = 10,replace = T), time=c(0,15), treatment=c("S","X"))
    #Add a variable to each table to id itself
    b$source<-"b"
    z$source<-"z"
    #concatenate the tables together
    all<-rbind(b,z)
    
    ggplot(all, aes(source, tracer, group=interaction(treatment,source), fill=treatment)) +
      geom_boxplot() + facet_grid(~time)
    

    【讨论】:

    • 谢谢你,非常感谢。我选择 Jaap 的响应是因为我可以毫不费力地遵循代码并将其直接应用于我的代码而无需替换任何东西。还有一张我所追求的图片,因此它在视觉上重申了代码有效(至少从新手的角度来看)
    猜你喜欢
    • 2016-09-14
    • 2011-12-17
    • 1970-01-01
    • 1970-01-01
    • 2017-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-03
    相关资源
    最近更新 更多