Reputation: 49033
I just met a strange behavior of facet_grid()
in ggplot2 0.9, and I wonder if someone could explain it to me...
Take the following df
data frame :
var <- sample(c("red", "blue"), 100, replace=TRUE)
group <- sample(c("group1", "group2"), 100, replace=TRUE)
df <- data.frame(var=factor(var), group=factor(group))
Which looks like :
var group
1 red group2
2 red group1
3 red group1
4 red group2
5 red group2
6 red group1
If I draw a bar chart of var
faceted by group
, I get a rather strange set of y-values :
ggplot(data=df, aes(x=var)) + geom_bar() + facet_grid(~group)
It seems strange because the y-values seem correct if I use facet_wrap
instead of facet_grid
:
ggplot(data=df, aes(x=var)) + geom_bar() + facet_wrap(~group)
Moreover, I can get back correct values with facet_grid
if I introduce another dummy variable in the data frame :
df$tmp <- 1:nrow(df)
ggplot(data=df, aes(x=var)) + geom_bar() + facet_grid(~group)
So, is it some sort of bug, or a normal behavior that I didn't understand?
Upvotes: 3
Views: 270