R - ggplot2 - 获取两组之间差异的直方图

时间:2016-03-17 00:50:57

标签: r ggplot2

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

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

Overlapping histograms

所以我所拥有的是每个事件的频率或数量。 我想做的是在每个垃圾箱中区分两个事件。这可能吗?怎么样?

例如,如果我们做RED减去蓝色:

  • x = 2时的值为〜-10
  • x = 4时的值为~40 - 200 = -160
  • x = 6时的值为~190 - 25 = 155
  • x = 8时的值为~10

我更喜欢使用ggplot2这样做,但另一种方式可以。我的数据框设置了这个玩具示例之类的项目(尺寸实际上是25000行x 30列) 已编辑:以下是使用的示例数据 GIST Example < / p>

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(连续变量)。但我真正想要的是它们的频率之间的差异。

2 个答案:

答案 0 :(得分:2)

所以,为了做到这一点,我们需要确保&#34; bins&#34;我们用于直方图的指标变量的两个级别都相同。这是一个有点天真的解决方案(在基础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()的非图形输出可能会有所帮助。

答案 1 :(得分:0)

以下是根据要求使用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()

Histogram without showing differences

使用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()

Final histogram with bars showing the difference between two distributions.

当然,比例和图例仍然需要修复,但这是一个不同的主题。

相关问题