Hi.
I am trying to do histograms in lattice, and I want to get both
counts and percents in the same plot. To try to be clearer ---
there are 3 levels to my factor; I'd like to get a 2 x 3 array
of plots where the top row consist of histograms by counts and
the bottom consists of (the corresponding) histograms by percent.
I tried the following:
# Demo.
library(lattice)
set.seed(42)
XX <- data.frame(y=runif(300,0,10),a=factor(sample(letters[1:3],300,
TRUE,c(0.5,0.3,0.2))))
XX <- rbind(XX,XX)
XX$gps <- rep(c(1,2),each=300)
print(histogram(~y|a*gps,as.table=TRUE,data=XX, panel=function
(x,...,type) {
panel.histogram(x,...,type=c("count","percent")[gps])
}
))
But I got 2 x 3 array of plots each containing only the text string
``Error using packet
<1, ..., 6> object "gps" not found.''
I tried various other fiddle-arounds and got nowhere.
Is it possible to do what I want? If so, how?
Thanks.
cheers,
Rolf
######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
Lattice question.
3 messages · Rolf Turner, Felix Andrews
Modifying your example...
library(lattice)
set.seed(42)
XX <- data.frame(y=runif(300,0,10),a=factor(sample(letters[1:3],300,
TRUE,c(0.5,0.3,0.2))))
XX <- rbind(XX,XX)
XX$gps <- rep(c("count","percent"),each=300)
print(histogram(~y|a*gps,as.table=TRUE,data=XX,
panel=function(x,...,type) {
panel.histogram(x,...,type=c("count","percent")[current.row()])
}
))
but the use of current.row() breaks if the layout changes.
Also, the y scales are the same here for count and percent data, which
may not be appropriate. You may have to set the y scales for each
panel explicitly. Or, another option is:
a <- histogram(~y|a, data = XX, subset = gps=="count", type="count")
b <- histogram(~y|a, data = XX, subset = gps=="density", type="density")
library(latticeExtra)
update(c(count=a, density=b), layout=c(3,2), ylab=c("count", "density"))
2009/3/30 Rolf Turner <r.turner at auckland.ac.nz>:
Hi.
I am trying to do histograms in lattice, and I want to get both
counts and percents in the same plot. To try to be clearer ---
there are 3 levels to my factor; I'd like to get a 2 x 3 array
of plots where the top row consist of histograms by counts and
the bottom consists of (the corresponding) histograms by percent.
I tried the following:
# Demo.
library(lattice)
set.seed(42)
XX <- data.frame(y=runif(300,0,10),a=factor(sample(letters[1:3],300,
TRUE,c(0.5,0.3,0.2))))
XX <- rbind(XX,XX)
XX$gps <- rep(c(1,2),each=300)
print(histogram(~y|a*gps,as.table=TRUE,data=XX,
panel=function(x,...,type) {
panel.histogram(x,...,type=c("count","percent")[gps])
}
))
But I got 2 x 3 array of plots each containing only the text string ``Error
using packet
<1, ..., 6> object "gps" not found.''
I tried various other fiddle-arounds and got nowhere.
Is it possible to do what I want? If so, how?
Thanks.
cheers,
Rolf
######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Felix Andrews / ??? mobile: +61_410400963 http://www.neurofractal.org/felix/ 3358 543D AAC6 22C2 D336 80D9 360B 72DD 3E4C F5D8
Thanks very much for your input.
On 30/03/2009, at 12:29 PM, Felix Andrews wrote:
Modifying your example...
library(lattice)
set.seed(42)
XX <- data.frame(y=runif(300,0,10),a=factor(sample(letters[1:3],300,
TRUE,c(0.5,0.3,0.2))))
XX <- rbind(XX,XX)
XX$gps <- rep(c("count","percent"),each=300)
print(histogram(~y|a*gps,as.table=TRUE,data=XX,
panel=function(x,...,type) {
panel.histogram(x,...,type=c("count","percent")
[current.row()])
}
))
but the use of current.row() breaks if the layout changes.
I kind of get the feeling that doing what I want will require constant ad hoc adjustment, so this is not really a worry.
Also, the y scales are the same here for count and percent data, which may not be appropriate.
Indeed. The thought had crossed my mind ....
You may have to set the y scales for each panel explicitly.
Yes, I'd like to ... but I can't figure out how. I tried a bit of an experiment and got the impression that panel.histogram() did not respond to specifications of ylim. I may just have done something silly ....
Or, another option is:
a <- histogram(~y|a, data = XX, subset = gps=="count", type="count")
b <- histogram(~y|a, data = XX, subset = gps=="density",
type="density")
library(latticeExtra)
update(c(count=a, density=b), layout=c(3,2), ylab=c("count",
"density"))
That looks very interesting; I'll give it a try.
Thanks.
cheers,
Rolf
######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}