I'm trying to combine 3 different histogram plots upon each other as one plot.
I use ggplot for that.
Here is my code:
library(ggplot2)
P<-read.table("11.txt",sep="",header=F)
N<-read.table("22.txt",sep="",header=F)
D<-read.table("33.txt",sep="",header=F)
# Converted into list
Ps = unlist(P)
Non = unlist(N)
Ds = unlist(D)
dat1 <- data.frame(dens1 = c(Ps), lines1 = rep(c("P"),by=112))
dat2 <- data.frame(dens2 = c(Ds), lines2 = rep(c("D"),by=459))
dat3 <- data.frame(dens3 = c(Non), lines3 = rep(c("N"),by=3340))
dat1$veg <- 'P'
dat2$veg <- 'D'
dat3$veg <- 'N'
colnames(dat1) <- c("x","Y")
colnames(dat2) <- c("x","Y")
colnames(dat3) <- c("x","Y")
# Plot each histogram
ggplot(dat1, aes(dat1$x, fill = dat1$Y)) + geom_histogram(bins = 150,alpha = 0.3, aes(y = (..count..)/sum(..count..)), position = 'identity') + scale_y_continuous(labels = percent, limits = c(0,1)) + labs(x="X") + theme(panel.border = element_rect(colour = "black"),panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) + theme_bw()+theme(legend.title=element_blank())
ggplot(dat2, aes(dat2$x, fill = dat2$Y)) + geom_histogram(bins = 150,alpha = 0.3, aes(y = (..count..)/sum(..count..)), position = 'identity') + scale_y_continuous(labels = percent, limits = c(0,1)) + labs(x="X") + theme(panel.border = element_rect(colour = "black"),panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) + theme_bw()+theme(legend.title=element_blank())
ggplot(dat3, aes(dat3$x, fill = dat3$Y)) + geom_histogram(bins = 150,alpha = 0.3, aes(y = (..count..)/sum(..count..)), position = 'identity') + scale_y_continuous(labels = percent, limits = c(0,1)) + labs(x="X") + theme(panel.border = element_rect(colour = "black"),panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) + theme_bw()+theme(legend.title=element_blank())
# To combine all histograms
data = rbind(dat1, dat2, dat3)
# Draw them all as a one plot
ggplot(data, aes(data$x, fill = data$Y)) + geom_histogram(bins = 150,alpha = 0.3, aes(y = (..count..)/sum(..count..)), position = 'identity') + scale_y_continuous(labels = percent, limits = c(0,1)) + labs(x="X") + theme(panel.border = element_rect(colour = "black"),panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) + theme_bw()+theme(legend.title=element_blank())
And here is the data in 11.txt
and 22.txt
and 33.txt
, they are diffeent in number.
11.txt:
2.98669E-06
3.37203E-06
7.0028E-06
8.50885E-06
8.71491E-06
8.9869E-06
9.59295E-06
9.96175E-06
9.97605E-06
1.00225E-05
9.59295E-06
9.59295E-06
22.txt:
6.07E-09
1.07E-08
1.18E-08
1.41E-08
1.57E-08
1.57E-08
1.68E-08
1.75E-08
1.77E-08
1.95E-08
1.77E-08
33.txt:
1.93E-07
2.25E-07
2.84E-07
3.00E-07
3.38E-07
4.33E-07
4.87E-07
5.20E-07
5.23E-07
5.46E-07
5.23E-07
4.33E-07
2.84E-07
And here are the 3 different histograms:
And here when I combine all in one plot:
As we can see, it seems that it sums altogether (we can notice that for example group N when plotting it alone and when it's combined), what I want is combine/add each of these plots onto/above each other.
Any help, please?