John Horton
John Horton

Reputation: 4282

Trying to keep filled bars in a faceted plot

Not sure what I'm doing wrong here. I have this plot:

ggplot(data.PE5, aes(ybands,fill=factor(decide))) + geom_bar(position="dodge") 

which produces:

Then I want to facet by a factor, creating two stacked plots w/ dodged, colored bars

ggplot(data.PE5, aes(ybands,fill=factor(decide))) + geom_bar(position="dodge") + 
facet_grid(~group_label) 

However, I lose the factor-based coloring, which I want to keep:

Upvotes: 4

Views: 153

Answers (1)

DrewConway
DrewConway

Reputation: 5457

If you move the fill into the geom_bar it should work. As:

ggplot(data.PE5, aes(ybands)) + geom_bar(aes(fill=factor(decide)),position="dodge") + facet_grid(~group_label)

The reason is the way ggplot2 builds plots as a grammar (I think).

Upvotes: 2

Related Questions