side-by-side histogram with different data size | R using ggplot2


I was working with ggplot2 and especially histogram, I have 3 different histograms each with different size (1st: n = 12, 2nd: n = 11, 3rd: n = 13).

I combined them using grid.arrange(g1, g2, g3, ncol = 1) which's fine.

But to be more comparative, I need to put them together (side-by-side) like this plot (with a percentage in my case as y-axis)

enter image description here

* Note I have different sizes

This is my code:

library(ggplot2)

P<-read.table("try11.txt", sep = "", header = F)
N<-read.table("try22.txt", sep = "", header = F)
D<-read.table("try33.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("A"), by = length(Ps)))
dat2 <- data.frame(dens2 = c(Ds), lines2 = rep(c("B"), by = length(Ds)))
dat3 <- data.frame(dens3 = c(Non), lines3 = rep(c("C"), by = length(Non)))

dat1$veg <- 'A'
dat2$veg <- 'B'
dat3$veg <- 'C'

colnames(dat1) <- c("x", "Y")
colnames(dat2) <- c("x", "Y")
colnames(dat3) <- c("x", "Y")

# Plot each histogram 
g1 <- ggplot(dat1, aes(dat1$x, fill = dat1$Y)) +
  geom_histogram(bins = 150, alpha = 0.3, color = "orange",
                 aes(y = (..count..)/sum(..count..)), position = 'identity') +
  scale_x_continuous(trans='log10') +
  scale_y_continuous(labels = percent, limits = c(0,1)) +
  labs(x = "", y = "") +
  theme_bw() +
  theme(panel.border = element_rect(colour = "black"),
        panel.grid.minor = element_blank(),
        axis.line = element_line(colour = "black"),
        legend.title = element_blank())

g2 <- ggplot(dat2, aes(dat2$x, fill = dat2$Y)) +
  geom_histogram(bins = 150,alpha = 0.3, color="purple", aes(y = (..count..)/sum(..count..)),
                 position = 'identity') +
  scale_x_continuous(trans = 'log10') +
  scale_y_continuous(labels = percent, limits = c(0,1)) +
  labs(x = "") +
  theme_bw() +
  theme(panel.border = element_rect(colour = "black"),
        panel.grid.minor = element_blank(),
        axis.line = element_line(colour = "black"),
        legend.title=element_blank())

g3 <- ggplot(dat3, aes(dat3$x, fill = dat3$Y)) +
  geom_histogram(bins = 150,alpha = 0.3, color="black",
                 aes(y = (..count..)/sum(..count..)), position = 'identity') +
  scale_x_continuous(trans = 'log10') +
  scale_y_continuous(labels = percent, limits = c(0,1)) +
  labs(x="X Values", y="") +
  theme_bw() +
  theme(panel.border = element_rect(colour = "black"),
        panel.grid.minor = element_blank(),
        axis.line = element_line(colour = "black"),
        legend.title = element_blank())

library(gridExtra)
grid.arrange(g1, g2, g3, ncol = 1)

And here is my input files:

try11.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

try22.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

try33.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 this what I got: enter image description here

I'm new to R to know those more complicated functionalities, any help will be appreciated.


Answers:


Not sure if that's what you mean:

require(tidyverse)
options(scipen = 999)

df1 <- data.frame(x = c(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))

df2 <- data.frame(x = c(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))

df3 <- data.frame(x = c(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))

rbind(df1 %>% 
    mutate(var = "df1"),
  df2 %>% 
    mutate(var = "df2"), 
  df3 %>% 
    mutate(var = "df3")) %>% 

ggplot(aes(x, group = var, color = var, fill = var, alpha = 0.2))+ geom_histogram(aes(y = (..count..)/sum(..count..))) + scale_y_continuous(labels= scales::percent)

enter image description here