I currently have this barplot in R (created with ggplot2
)
I need to have it like this:
I tried to use RColorBrewer package, but it didn't work since the maximum amount of colours in the coresponding palette is 11. Any ideas?
pal <- brewer.pal(11,"RdYlGn")
ggplot(data = bm_mod, aes(x = bm_mod$country, y = bm_mod$V)) +
geom_bar(stat = "identity", colour = pal, fill = pal) + coord_flip() +
labs(y="Under/over valuation in %", x="")
Here is the data: link
Update: I tried it again with this code:
ggplot(data = bm_mod, aes(x = country, y = V)) +
geom_bar(stat = "identity", fill = country, show_guide = FALSE) +
coord_flip() + scale_fill_manual(values = colorRampPalette(brewer.pal(11,"RdYlGn"))(nrow(bm_mod))) +
labs(y="Under/over valuation in %", x="")
However the output looks pretty weird. Is it a problem with the brewer or another mistake?
A few points:
As Nick noted, you should not be using bd_mod$
to refer to variables inside of aes()
. The whole point of specifying data = bm_mod
is that is frees you from having to type bm_mod$
over and over again.
Secondly, your attempt at setting fill = pal
is somewhat confused. Specifying an aesthetic outside of aes()
means you are setting it to a specific value, so that should only be done with a single value, e.g. fill = "blue"
.
What you really want is a different fill for each x value, so you should be mapping fill to country
and then setting the color scheme in scale_fill_manual
.
When you ask questions like these, you should always provide a reproducible example. In this case, that would have been very easy, something like this:
df <- data.frame(x = letters,y = runif(26))
ggplot(df,aes(x = x,y = y)) +
geom_bar(aes(fill = x),stat = "identity",show_guide = FALSE) +
scale_fill_manual(values = colorRampPalette(brewer.pal(11,"RdYlGn"))(26))
I've used colorRampPalette
to interpolate the colors in the brewer palette you would like to use.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With