Adhil
Adhil

Reputation: 1858

How to create a Multi panel Histograms with common X Axis in R

This is my code:

library(ggplot2)

ifile <- read.table("C:/files/text.lu", skip = 2, header = TRUE, sep="\t")
ifileVf2 <- data.frame(ifile["Vf2"], ifile["Site"])

quantiles <- quantile(ifileVf2$Vf2[ifileVf2$Site == '1'], c(0.001, 0.999))
Site_1_Values <- hist(ifileVf2$Vf2[(ifileVf2$Site == '1') & (ifileVf2$Vf2 > quantiles[1]) & (ifileVf2$Vf2 < quantiles[2])], breaks=150, freq=TRUE, right=TRUE)
quantiles <- quantile(ifileVf2$Vf2[ifileVf2$Site == '2'], c(0.001, 0.999))
Site_2_Values <- hist(ifileVf2$Vf2[(ifileVf2$Site == '2') & (ifileVf2$Vf2 > quantiles[1]) & (ifileVf2$Vf2 < quantiles[2])], breaks=150, freq=TRUE, right=TRUE)

par(mfrow=c(2,1))
plot( Site_1_Values, col=rgb(0,0,1,1/4),  panel.first = grid())
plot( Site_2_Values, col=rgb(0,1,0,1/4), add=T,  panel.first = grid())

This is the Result:

enter image description here How do i get two histograms into different panels but common x-axis?

Upvotes: 0

Views: 2035

Answers (1)

Jack Brookes
Jack Brookes

Reputation: 3830

library(ggplot2)

# example data
dat <- data.frame(
  voltage = c(rnorm(500, 0, 10), y = rnorm(500, 10, 10)),
  site = c(rep("site1", 500), rep("site2", 500))
)

# plot
ggplot(dat, aes(x = voltage, fill = site)) + 
  geom_histogram() + 
  facet_grid(site ~ .)

enter image description here

Upvotes: 2

Related Questions