Strange behavior of facet_grid ()
I just came across strange behavior facet_grid()
in ggplot2 0.9 and I am wondering if someone can explain this to me ...
Take the following data frame df
:
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))
What looks like:
var group
1 red group2
2 red group1
3 red group1
4 red group2
5 red group2
6 red group1
If I plot the var
marked bar chart , 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 to be correct if I use facet_wrap
instead facet_grid
:
ggplot(data=df, aes(x=var)) + geom_bar() + facet_wrap(~group)
Also, I can return the correct values with facet_grid
if I inject another dummy variable into the dataframe:
df$tmp <- 1:nrow(df)
ggplot(data=df, aes(x=var)) + geom_bar() + facet_grid(~group)
So, is this some kind of bug or normal behavior that I didn't understand?
source to share