2

I am manually setting the order of my histogram bars in ggplot2, so they don't just list in alphabetical order from left to right using something like:

p + xlim("second", "first", "third")

Then when I set the color with

p + scale_fill_grey()

the bars are colored in alphabetical order darkest to lightest. Can I get them to fill in order from "second" to "first" to "third"? Or even group different bars as the same color?

The Nightman
  • 5,609
  • 13
  • 41
  • 74

2 Answers2

3

I think you can change the ordering of colours manually using limits() in scale_fill_grey(). That should overrule the standard order and apply the order you've set manually using limits().

p + scale_fill_grey(limits = c("second", "first", "third")) 
2

I am not entire sure what you are asking; I am confused. But, if you use scale_fill_grey(), whatever comes first on the x-axis will be dark. That is, even when you reorder the levels of your factor for the axis, you will end up seeing the first bar plot has dark colour. As long as I see from your question, you may want to manually assign colours to bars. Hope this will let you move forward.

# I create a sample data, which you should provide when you ask a question.
location <- c("London", "Paris", "Madrid")
value <- runif(3, 15,30)

foo <- data.frame(location, value, stringsAsFactors = F)
foo$location <- factor(foo$location)

# Colors are assigned in the alphabetical order of the cities.

ggplot(foo, aes(x = location, y = value, fill = location)) +
    geom_bar(stat="identity") +
    scale_fill_manual(values=c("black", "grey", "white"))

enter image description here

Now I change the order of the cities and draw another figure. But, the colors are still assigned in the alphabetical order.

foo$location <- factor(foo$location, levels = c("Paris", "Madrid", "London"))

ggplot(foo, aes(x = location, y = value, fill = location)) +
    geom_bar(stat="identity") +
    scale_fill_manual(values=c("white", "grey", "black"))

enter image description here

jazzurro
  • 23,179
  • 35
  • 66
  • 76
  • This is helpful. The manual coloring is exactly what I was looking for to color manually. But As for the automatic color filling, I am looking for something slightly different. In your example, the first plot has black gray white. Then when you reorder the bars, London is still black, Madrid gray and Paris white. Is there a way to automatically have the leftmost bar be black, middle be gray and rightmost white regardless of what order you put the bars in? Thanks. – The Nightman Sep 11 '14 at 04:25
  • @TheNightman Now what you want it getting clear to me. If that is what you want, you want to use `scale_fill_grey()` in the second example. Then, whatever city appears on the left-edge will have the darkest colour as you wish. – jazzurro Sep 11 '14 at 04:44
  • so 'scale_fill_grey()' was what I was using, but this fills the bars in alphabetical order regardless of their order along the x-axis. Is there something I need to pass to 'scale_fill_grey()' to make it fill in left to right? – The Nightman Sep 11 '14 at 15:09
  • @TheNightman It would have been really helpful if you would mention these things in your question. Since I do not have your data and code, I have a hard time to guess. But, I have the feeling that you have character not factor, maybe. – jazzurro Sep 11 '14 at 15:25