Reputation: 5769
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?
Upvotes: 2
Views: 15724
Reputation: 31
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"))
Upvotes: 3
Reputation: 23574
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"))
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"))
Upvotes: 3