6
par(mfrow=c(1,2))
plot(1:12, log = "y")
plot(1:2, xaxs = "i")

enter image description here

However, when I try to do a side-by-side densityplot the plots get output seperately:

# load the stud.recs dataset
library(UsingR)

par(mfrow=c(1,2))
densityplot(stud.recs$sat.v)
densityplot(stud.recs$sat.m)

Why is par(mfrow=c(1,2)) not working for the density plots?

Chris Snow
  • 23,813
  • 35
  • 144
  • 309
  • 1
    Note: `densityplot` is not documented in `UsingR` but does exist. There is also `DensityPlot` but it uses base graphics. `densityplot` is also not in the namespace so I'm not sure how it is exposed. Separate issue from this question of course. – Bryan Hanson Jun 28 '15 at 11:46
  • 1
    OK. Got it, red-herring as they say. `UsingR` has nothing to do with this question. `densityplot` belongs to `lattice`. – Bryan Hanson Jun 28 '15 at 11:52
  • upvoted your first comment for pointing me to `DensityPlot` which worked as I expected `densityplot` to work – Chris Snow Jun 28 '15 at 13:12

1 Answers1

9

densityplot produces lattice plots (which are different to base plots).

So in order to have them side by side you need to do:

library(UsingR)
par(mfrow=c(1,2))
a <- densityplot(stud.recs$sat.v)
b <- densityplot(stud.recs$sat.m)

#this is the print.lattice method below
# ?print.trellis for help
print(a, position = c(0, 0, 0.5, 1), more = TRUE)
print(b, position = c(0.5, 0, 1, 1))

enter image description here

LyzandeR
  • 37,047
  • 12
  • 77
  • 87