3

I have a three facet figure with geombars in each.

Each facet represents a variable that I've used in other graphs where ggplot2 assigns them fill colors by default.

The faceted plots are black by default. I can change the fill color for all three panels but haven't found how to change each panel's fill to a different color.

Here is arbitrary data and code to represent the basic idea:

df = data.frame(matrix(data=c(1,3,1,2,3,1,2,3,2,3,1,2,3,1,2,3,
                              1,3,2,1,2,2,2,3,3,3,1,1,1,3,3,2), 
                       ncol=2,byrow=TRUE))
dimnames(df)[[2]] =c("x","y")

dodgebars <- 
  ggplot(data = df,aes(factor(y),fill=factor(x))) +
  geom_bar(aes(group=factor(x)),
           position="dodge")

facetedbars <- 
  ggplot(data = df, aes(factor(y))) +
  geom_bar(aes(group=x)) +
  facet_grid(~x)

How can the color of each facet be matched to it's fill cover in "dodgebars?"

kshrek
  • 55
  • 1
  • 5
  • 3
    Welcome to SO. Generally, you should provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to get the most out of it. To your problem: what if you map the facetting variable to fill colour? Then, every facet should get a different fill colour (if that's what you want). – lukeA Feb 05 '15 at 19:39
  • I've provided for making very basic plots. Could you also provide code for "mapping the facetting variable to fill colour?" I've been going through the ggplot documentation but am not sure where to look. – kshrek Feb 05 '15 at 20:38
  • `facetedbars + aes(fill = as.factor(x))`? – lukeA Feb 05 '15 at 20:40
  • I'm sorry. I'm not that inexperienced with ggplot. That's embarrassing. Thanks for your patience! – kshrek Feb 05 '15 at 21:08
  • 1
    You're welcome. Does that solve your problem? In that case, I'd submit it as an answer, so you can check it and close the question. – lukeA Feb 06 '15 at 09:21
  • It does. Go ahead and submit it. – kshrek Feb 06 '15 at 19:32

1 Answers1

5

Map the facetting variable to fill colour. Then, every facet gets a different bar colour:

facetedbars + aes(fill = as.factor(x))
lukeA
  • 53,097
  • 5
  • 97
  • 100