1

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: enter image description here

enter image description here

enter image description here

And here when I combine all in one plot: enter image description here

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?

LamaMo
  • 576
  • 2
  • 8
  • 19

1 Answers1

0

I think you are looking for facet_wrap (or facet_grid)

If you add +facet_wrap(~Y, ncol = 1, scales = 'free') to your code it should give you the desired result. enter image description here

konvas
  • 14,126
  • 2
  • 40
  • 46
  • Do you mean you just don't want the space between the panels? You could do for example `+facet_wrap(~Y, ncol = 1, strip.position = 'left') + theme(panel.spacing = unit(0, 'lines'))` which will join the panels. If you want you can get rid of strips altogether by playing with the `strip.*` arguments of `theme`. – konvas Oct 07 '18 at 16:30
  • Sorry I'm not sure I understand, perhaps you mean you want each panel to have a different x-axis? try `+facet_wrap(~Y, ncol = 1, scales = 'free')` – konvas Oct 07 '18 at 16:55
  • Could you please see the difference in group N without combing and when it combined, not all it values shown, it seems that this combination sum all groups one each as it is when it plotted by itself. See the plot in the second answer https://stackoverflow.com/questions/3541713/how-to-plot-two-histograms-together-in-r – LamaMo Oct 07 '18 at 17:03
  • This is because when you plot them all together, the "bins" are calculated across all three groups, and it so happens that all elements of group N fall inside the same bin (this is why they appear as one tall bar in your combined histogram). By setting `scales='free'` inside `facet_wrap` the bins will be calculated for each group separately, giving you exactly what you had in the three individual plots (now in the same plot, but 3 different panels). I've updated the answer. – konvas Oct 07 '18 at 17:09
  • You are right, but did you try to plot it? I try it but when I compare it to the individual one still differ. please try and see what I mean (I cannot attach the figure here) – LamaMo Oct 07 '18 at 17:32
  • I did try to plot it (it's the image attached to my answer). They look the same to me (same number of bars and heights)... – konvas Oct 07 '18 at 17:52
  • Could you compare it to any of the individual plot? for example first bar in N group is near to 13 but here in the combined version seems to more less – LamaMo Oct 07 '18 at 17:55
  • Ah sorry, you're right I missed that. It's 1/3rd of the height because I suppose `sum(..count..)` calculates the sum over all groups. So `aes(y = 3*(..count..)/sum(..count..))` should do the trick.. not sure how to make this more elegant though – konvas Oct 07 '18 at 18:08
  • Now it looks perfect, one more thing as I said before. Is it possible to combine all as a one (so they overlap through each other)? – LamaMo Oct 07 '18 at 18:13
  • Also what happen to aes(y = 3*(..count..)/sum(..count..)) if the number of data differ in each group? that what I try it now on more values and they differ per each group :( – LamaMo Oct 07 '18 at 19:08