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
scale_fill_manual()
and others would accept an incomplete vector of specs, e.g.scale_fill_manual(values=c('-1'='black'))
and understand that the other colors are to be picked automatically as usual. – Depute