r - In ggplot2 change colour fill order -


i manually setting order of histogram bars in ggplot2, don't list in alphabetical order left right using like:

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

then when set color with

p + scale_fill_grey() 

the bars colored in alphabetical order darkest lightest. can them fill in order "second" "first" "third"? or group different bars same color?

i not entire sure asking; confused. but, if use scale_fill_grey(), whatever comes first on x-axis dark. is, when reorder levels of factor axis, end seeing first bar plot has dark colour. long see question, may want manually assign colours bars. hope let move forward.

# create sample data, should provide when ask question. location <- c("london", "paris", "madrid") value <- runif(3, 15,30)  foo <- data.frame(location, value, stringsasfactors = f) foo$location <- factor(foo$location)  # colors assigned in alphabetical order of 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 change order of cities , draw figure. but, colors still assigned in 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


Comments

Popular posts from this blog

c# - HttpResponseMessage System.InvalidOperationException -

sql - Postgresql error: "failed to find conversion function from unknown to text" -