juba
juba

Reputation: 49033

Strange behavior of facet_grid()

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)

plot1

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)

plot2

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)

plot3

So, is it some sort of bug, or a normal behavior that I didn't understand?

Upvotes: 3

Views: 270

Answers (1)

csgillespie
csgillespie

Reputation: 60452

This bug has been in fixed in version 0.9.1 of ggplot2.

Upvotes: 3

Related Questions