See if the codes below help you.
I created three samples x
, y
and z
, each with different normal distributions, and plotted next to each other. Note that I first created x
and y
, only to then add z
. Note also that the facet_grid
function does not require any parameters to leave the y-axes on the same scale.
# dados originais
n <- 1000
x <- rnorm(n, mean=5, sd=3)
y <- rnorm(n, mean=0, sd=1)
dados <- data.frame(grupos=rep(c("x", "y"), each=n), valores=c(x,y))
ggplot(dados, aes(x=valores)) +
geom_histogram(bins=30) +
facet_grid(~ grupos)
#adicionandooutrogrupoz<-rnorm(n,mean=10,sd=2)z<-data.frame(grupos="z", valores=z)
dados <- rbind(dados, z)
ggplot(dados, aes(x=valores)) +
geom_histogram(bins=30) +
facet_grid(~ grupos)
Ifyouwanttoleavethexaxesallonthesamescale,justrunthecodebelowwiththeoptionscales="free_x"
within facet_grid
:
ggplot(dados, aes(x=valores)) +
geom_histogram(bins=30) +
facet_grid(~ grupos, scales = "free_x")