[R] Joining Histograms Into a Figure

Dieter Menne dieter.menne at menne-biomed.de
Mon May 26 08:33:57 CEST 2008


Edward Wijaya <ewijaya <at> gmail.com> writes:

> 
> I have two histograms created separately using
> the following code. It creates two separate figures.
> 
> dat <- read.table(file="GDS1096.modelout", head = FALSE )
> 
> __BEGIN__
> dat <- read.table(file="GDS1096.modelout", head = FALSE )
> 
> hist(dat$V2, main="AIC Freq", xlab = "\# Component", breaks = 36, xlim =
> c(0,max(dat$V2)), col = "dark red", freq = TRUE)
> hist(dat$V3, main="BIC Freq", xlab = "\# Component", breaks = 36, xlim =
> c(0,max(dat$V2)), col = "blue", freq = TRUE)
> __END__
> 
> How can I joint this two histograms into one figure?

You could use par(mfcol), and if you want the plots closely spaced, play with
the margin parameters documented under par(). In most cases, the result is not
that satisfactory, so I would recommend using lattice graphics instead, where
the pairing is a build-in feature.
Dieter


par(mfcol=c(1,2))
a = rnorm(100)
b = rnorm(100)
hist(a)
hist(a)

library(lattice)
dt = data.frame(val =c(a,b),group=rep(c("A","B"),each=100))
histogram(~val|group,data=dt)



More information about the R-help mailing list