I have the following graph based on this dataset
My goal is to match the colors on each bar to the "TAG" color name.
I have tried to use a palette
palette <- RColorBrewer::brewer.pal(length(unique(tidied_pca$Tag)),name = 'Set1')
but i get this error
Warning message:
In RColorBrewer::brewer.pal(length(unique(tidied_pca$Tag)), name = "Set1") :
n too large, allowed maximum for palette Set1 is 9
Returning the palette you asked for with that many colors
The original code to render the graph is here:
tidied_pca %>%
filter(PC == "PC2") %>%
top_n(40, abs(Contribution)) %>%
mutate(Tag = reorder(Tag, Contribution)) %>%
ggplot(aes(Tag, Contribution, fill = Tag)) +
geom_col(show.legend = FALSE, alpha = 0.8) +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5),
axis.ticks.x = element_blank()) +
labs(x = "Bottle Color percentages",
y = "Relative importance in principle component",
title = "What color accounts for most variation PCA2")
To just use the colors in the tag column, add
+ scale_fill_identity()
But for the actual error message, there's no color brewer palette that has that many distinct colors. For Set1
, the max colors you get is 9. You could interpolate between those values to get more colors, but then you will probably loose the nice property of having easily distinguishable colors. You can do that with the base R colorRampPalette
function.
palette <- colorRampPalette(RColorBrewer::brewer.pal(9,name = 'Set1'))(length(unique(tidied_pca$Tag)))