Question

SO I am trying to plot multiple conditioned histograms in one page. I have three numeric predictors, AFE, ZCR, and ACC, and they need to be conditioned on class either 0 or 1.

When I do it for a single variable:

histogram(myData.toplot$Class ~ myData.toplot$AFE | myData.toplot$Class)

It works fine. Now I want to put all three of them in the same plot. I tried this:

x = histogram(myData.toplot$Class ~ myData.toplot$AFE | myData.toplot$Class)
y = histogram(myData.toplot$Class ~ myData.toplot$ZCR | myData.toplot$Class)
z = histogram(myData.toplot$Class ~ myData.toplot$ACC | myData.toplot$Class)
print(x, position=c(0, 0, 1, .35), more=TRUE)
print(y, position=c(0, .3, 1, .65), more=TRUE)
print(z, position=c(0, .6, 1, .95))

And that works, but, I don't want to have to specify the exact placing c, as the number of predictors grows. So instead, I tried the layout function:

histogram( myData.toplot$Class ~ myData.toplot$AFE +myData.toplot$ZCR +myData.toplot$ACC | myData.toplot$Class ,layout=c(1,3),data = myData.toplot)

However, that is very unsatisfactory. It doesn't know how to condition on the classes properly. How can I use the lattice package to plot three panels of conditioned histograms in one page?

There is another way to do histograms:

set.seed(42)
p1 <- hist(rnorm(500,4))                     # centered at 4
p2 <- hist(rnorm(500,6))                     # centered at 6
plot( p1, col=rgb(0,0,1,1/4), xlim=c(0,10))  # first histogram
plot( p2, col=rgb(1,0,0,1/4), xlim=c(0,10), add=T)  # second

However when I add:

par(mfrow=c(3,1))  ##this is what I added
set.seed(42)
p1 <- hist(rnorm(500,4))                     # centered at 4
p2 <- hist(rnorm(500,6))                     # centered at 6
plot( p1, col=rgb(0,0,1,1/4), xlim=c(0,10))  # first histogram
plot( p2, col=rgb(1,0,0,1/4), xlim=c(0,10), add=T)  # second

set.seed(41)
p11 <- hist(rnorm(500,4))                     # centered at 4
p21 <- hist(rnorm(500,6))                     # centered at 6
plot( p11, col=rgb(0,0,1,1/4), xlim=c(0,10))  # first histogram
plot( p21, col=rgb(1,0,0,1/4), xlim=c(0,10), add=T)  # second

set.seed(40)
p12 <- hist(rnorm(500,4))                     # centered at 4
p22 <- hist(rnorm(500,6))                     # centered at 6
plot( p12, col=rgb(0,0,1,1/4), xlim=c(0,10))  # first histogram
plot( p22, col=rgb(1,0,0,1/4), xlim=c(0,10), add=T)  # second

I get two pages, one with p1, p2, and the combo of p1 and p2, and another page with p11, p12, and the combo of p11 and p12...etc I want to use the par() command to simply put the combos in three rows. How can I do that?

Était-ce utile?

La solution

The problem is due to hist creating a plot. Use the argument plot = FALSE and it will work.

par(mfrow=c(3,1))  ##this is what I added
set.seed(42)
p1 <- hist(rnorm(500,4), plot = FALSE)                     # centered at 4
p2 <- hist(rnorm(500,6), plot = FALSE)                     # centered at 6
plot( p1, col=rgb(0,0,1,1/4), xlim=c(0,10))  # first histogram
plot( p2, col=rgb(1,0,0,1/4), xlim=c(0,10), add=T)  # second

set.seed(41)
p11 <- hist(rnorm(500,4), plot = FALSE)                     # centered at 4
p21 <- hist(rnorm(500,6), plot = FALSE)                     # centered at 6
plot( p11, col=rgb(0,0,1,1/4), xlim=c(0,10))  # first histogram
plot( p21, col=rgb(1,0,0,1/4), xlim=c(0,10), add=T)  # second

set.seed(40)
p12 <- hist(rnorm(500,4), plot = FALSE)                     # centered at 4
p22 <- hist(rnorm(500,6), plot = FALSE)                     # centered at 6
plot( p12, col=rgb(0,0,1,1/4), xlim=c(0,10))  # first histogram
plot( p22, col=rgb(1,0,0,1/4), xlim=c(0,10), add=T)  # second

enter image description here

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top