(Trying a more specific list, apologies to r-help@ subscribers that may have already seen this.)
I'm a new ggplot user and am really loving it so far, but I have run into a little trouble. I'm hoping someone here can clarify.
I've been plotting histograms of discrete variables like so:
> df<-data.frame(names=c("Bob","Mary","Joe","Bob","Bob"))
> p<-ggplot(df,aes(names))
> p+geom_histogram()
But sometimes I want to constrain the categories (the factor in my real dataset has many, many levels). When I do this in (what seems like) the simplest way I get an error:
> p+geom_histogram()+xlim("Bob","Mary")
Error in data.frame(count = as.numeric(tapply(weight, bins, sum, na.rm = TRUE))$
arguments imply differing number of rows: 0, 1
If I use xlim() to specify all the levels, or all the levels plus a few extra levels, things work as expected:
> p+geom_histogram()+xlim("Bob","Mary","Joe")
> p+geom_histogram()+xlim("Bob","Mary","Joe","Frank")
I found the error surprising because I had been successfully plotting ..density.. instead of the default ..count.. and constraining the x-axis like so:
> p+geom_histogram(aes(y=..density..,group=1))+xlim("Bob","Mary")
After some experimentation, I've come to the conclusion that using the group aesthetic is the key to successfully using xlim() with stat_bin and discrete scales. That is, the following does what I want:
p+geom_histogram(aes(group=1))+xlim("Bob","Mary")
I have two questions:
1) Is this expected behavior? The interaction of the group aesthetic and discrete scale manipulation seems unintuitive to me, but perhaps this is because I'm a ggplot novice.
2) More generally, how do people debug gpplot commands? When I get a cryptic error message like the above it's nearly always because I've asked for something unreasonable, but if often takes me a long time and lots of experimentation to spot my error. Do folks have any ggplot debugging tips?
Thanks very much for your help,
-J
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example:
http://gist.github.com/270442
To post: email
ggp...@googlegroups.com
To unsubscribe: email
ggplot2+u...@googlegroups.com
More options:
http://groups.google.com/group/ggplot2