I have a R dataframe (df) which I am plotting as a bar graph in ggplot2, and coloring based on a column in the dataframe (df$type
). Right now, I am using the default coloring pattern (scale_fill_brewer
) to assign colors.
How can I assign the color black to one value, ( df$type == -1
)and use scale_fill_brewer to assign the rest of the colors? (all other df$types
are a within a set of integers from 1 to X, where X is the number of unique values)
So far, I have been able to do this manually by figuring out the set of colors scale_fill_brewer
uses for N different items then predending the color black and passing that to scale_fill_manual
.
rhg_cols1<- c("#000000","#F8766D","#7CAE00","#00BFC4","#C77CFF" )
ggplot(y=values,data=df, aes(x=name, fill=factor(type))) +
geom_bar()+ scale_fill_manual(values = rhg_cols1)
The problem is that I need a solution that works without manually assigning colors by using a hex color calculator to figuring out the hex values of scale_fill_brewer
.
something like:
ggplot(y=values,data=df, aes(x=name, fill=factor(type))) +
geom_bar()+ scale_fill_brewer(value(-1, "black")
Thank you!
EDIT: The solution must work for more than 30 colors and work for "Set2" of ColorBrewer
If you need to distinguish among this many (30+) different categories you probably need to back up and spend some more time thinking about the project strategically: it will be nearly impossible to come up with a set of 30 colo(u)rs that are actually distinguishable (especially in a way that is independent of platform/rendering channel).
There is basically no solution that will work with
Set2
and 30+ colours. Some of the CB palettes (Set3
andPaired
;library(RColorBrewer); display.brewer.all(n=12)
) allow as many as 12 colours.edit: the OP wants to do exploratory data analysis with good, distinguishable colours that won't break if there happen to be a lot of categories. I would suggest something along these lines:
I think this works reasonably well (you could substitute
Set3
and a cutoff of 13 colours if you preferred). The only drawback (that I can think of) is the discontinuity between the plots with 9 and 10 colours.Coming up with a better solution for picking sets of N distinguishable colours in a programmatic way is going to be pretty hard ...
The package
RColorBrewer
contains the palettes and you can use the functionbrewer.pal
to return a colour palette of your choice.For example, a sequential blue palette of 5 colours:
You can get a list of valid palette names in the
?brewer.pal
help files. These names correspond with the names at the ColorBrewer website.You can now use or modify the results and pass these to
ggplot
using thescale_manual_fill
as you suggested: