规范化不同的数据帧直方图并绘制在一起

时间:2016-09-07 08:50:56

标签: r ggplot2 histogram normalization

我有4个不同长度的数据帧(672000,240000,1600000,1940000)。

我希望在同一图表中表示4个直方图。为此,我使用了这种方法:

#1º. I create a data frame with the data of interest.    
dat1 = data.frame(x=distances_298$V1, group="alpha298")
dat2 = data.frame(x=distances_318$V1, group="alpha318")
dat3 = data.frame(x=distances_ext_278$V1, group="ext278")
dat4 = data.frame(x=distances_ext_298$V1, group="ext298")

#I use rbind.fill to bind data frames that don't have the same set of columns 
library(plyr)
dat <- rbind.fill(dat1,dat2,dat3,dat4)

# Normalize histogram. 
ggplot(dat, aes(x, fill=group, colour=group)) +
  geom_histogram(aes(y=..count../sum(..count..)), breaks=seq(0,15,0.1), alpha=0.6, 
  position="identity", lwd=0.1) +
  labs(x=expression(d["HB"]), y="Frecuency")  + geom_vline(xintercept = 2.5, colour="red") +
  scale_x_continuous(expand = c(0.01, 0), breaks = seq(0, 15, 2.5), limits = c(0,15)) +
  scale_y_continuous( limits = c(0,0.025), breaks = seq(0,0.025,0.005)) +
  scale_fill_manual(values = c("red", "green", "blue", "yellow")) + 
  ggtitle("(A)") +
  theme_minimal()+
  theme(legend.position = c(0.9, 0.8), legend.key.size = unit(0.5, "cm")) +
  theme(plot.title = element_text(hjust = -0.1, vjust=2.12))

我从这段代码得到的是:

enter image description here

但是,正如您所看到的,数据未在0-1之间标准化,特别是数据alpha318

在此链接中,您可以获得我正在使用的数据:Link

编辑:

使用geom_histogram(aes(y = ..density ..))

enter image description here

0 个答案:

没有答案