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
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…