【问题标题】:R - ggplot2 - Get histogram of difference between two groupsR - ggplot2 - 获取两组之间差异的直方图
【发布时间】:2016-03-17 00:50:57
【问题描述】:

假设我有一个包含两个重叠组的直方图。这是来自 ggplot2 的一个可能的命令和一个假装的输出图。

ggplot2(data, aes(x=Variable1, fill=BinaryVariable)) + geom_histogram(position="identity")

所以我所拥有的是每个事件的频率或计数。 我想做的是获取每个 bin 中两个事件之间的差异。这可能吗?怎么样?

例如,如果我们做 RED 减去 BLUE:

  • x=2 处的值约为 -10
  • x=4 处的值约为 40 - 200 = -160
  • x=6 处的值约为 190 - 25 = 155
  • x=8 处的值约为 10

我更喜欢使用 ggplot2 来执行此操作,但另一种方式也可以。我的数据框设置了类似这个玩具示例的项目(尺寸实际上是 25000 行 x 30 列)已编辑:这是要使用的示例数据 GIST Example

ID   Variable1   BinaryVariable
1     50            T          
2     55            T
3     51            N
..    ..            ..
1000  1001          T
1001  1944          T
1002  1042          N

从我的示例中可以看出,我对一个直方图很感兴趣,可以分别为每个 BinaryVariable(T 或 N)绘制 Variable1(一个连续变量)。但我真正想要的是它们的频率之间的差异。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    因此,为了做到这一点,我们需要确保我们用于直方图的“箱”对于指标变量的两个级别都是相同的。这是一个有点幼稚的解决方案(以R为基础):

    df = data.frame(y = c(rnorm(50), rnorm(50, mean = 1)),
                    x = rep(c(0,1), each = 50))
    #full hist
    fullhist = hist(df$y, breaks = 20) #specify more breaks than probably necessary
    #create histograms for 0 & 1 using breaks from full histogram
    zerohist = with(subset(df, x == 0), hist(y, breaks = fullhist$breaks))
    oneshist = with(subset(df, x == 1), hist(y, breaks = fullhist$breaks))
    #combine the hists
    combhist = fullhist
    combhist$counts = zerohist$counts - oneshist$counts
    plot(combhist)
    

    所以我们指定应该使用多少中断(基于完整数据的直方图的值),然后我们计算每个中断的计数差异。

    PS检查hist() 的非图形输出是什么可能会有所帮助。

    【讨论】:

      【解决方案2】:

      这是一个按要求使用ggplot 的解决方案。 关键思想是使用ggplot_build 得到由stat_histogram. 计算的矩形,从中您可以计算每个bin 中的差异,然后使用geom_rect. 创建一个新图

      使用对数正态数据设置和创建模拟数据集

      library(ggplot2)
      library(data.table)
      theme_set(theme_bw())
      n1<-500
      n2<-500
      k1 <- exp(rnorm(n1,8,0.7))
      k2 <- exp(rnorm(n2,10,1))
      df <- data.table(k=c(k1,k2),label=c(rep('k1',n1),rep('k2',n2)))
      

      创建第一个图

      p <- ggplot(df, aes(x=k,group=label,color=label)) + geom_histogram(bins=40) + scale_x_log10()
      

      使用ggplot_build获取矩形

      p_data <- as.data.table(ggplot_build(p)$data[1])[,.(count,xmin,xmax,group)]
      p1_data <- p_data[group==1]
      p2_data <- p_data[group==2]
      

      加入 x 坐标以计算差异。请注意,y 值不是计数,而是第一个图的 y 坐标。

      newplot_data <- merge(p1_data, p2_data, by=c('xmin','xmax'), suffixes = c('.p1','.p2'))
      newplot_data <- newplot_data[,diff:=count.p1 - count.p2]
      setnames(newplot_data, old=c('y.p1','y.p2'), new=c('k1','k2'))
      
      df2 <- melt(newplot_data,id.vars =c('xmin','xmax'),measure.vars=c('k1','diff','k2'))
      

      制作最后的情节

      ggplot(df2, aes(xmin=xmin,xmax=xmax,ymax=value,ymin=0,group=variable,color=variable)) + geom_rect()
      

      当然,比例和图例仍然需要修正,但这是另一个话题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-04-26
        • 1970-01-01
        • 2017-03-24
        • 2021-07-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多