Ggplot2 改变条形的颜色。错误 n 太大,调色板允许的最大值
Ggplot2 change colour of bars. Error n too large, allowed maximum for palette
我根据此 dataset
得到以下图表
我的目标是将每个条上的颜色与“TAG”颜色名称相匹配。
我尝试过使用调色板
palette <- RColorBrewer::brewer.pal(length(unique(tidied_pca$Tag)),name = 'Set1')
但我得到这个错误
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
渲染图形的原始代码在这里:
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")
要仅使用标记列中的颜色,请添加
+ scale_fill_identity()
但对于实际的错误消息,没有颜色 brewer 调色板具有那么多不同的颜色。对于 Set1
,您获得的最大颜色是 9。您可以 可以 在这些值之间进行插值以获得更多颜色,但是您可能会失去很好的 属性具有易于区分的颜色。您可以使用基本 R colorRampPalette
函数来做到这一点。
palette <- colorRampPalette(RColorBrewer::brewer.pal(9,name = 'Set1'))(length(unique(tidied_pca$Tag)))
我根据此 dataset
得到以下图表我的目标是将每个条上的颜色与“TAG”颜色名称相匹配。
我尝试过使用调色板
palette <- RColorBrewer::brewer.pal(length(unique(tidied_pca$Tag)),name = 'Set1')
但我得到这个错误
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
渲染图形的原始代码在这里:
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")
要仅使用标记列中的颜色,请添加
+ scale_fill_identity()
但对于实际的错误消息,没有颜色 brewer 调色板具有那么多不同的颜色。对于 Set1
,您获得的最大颜色是 9。您可以 可以 在这些值之间进行插值以获得更多颜色,但是您可能会失去很好的 属性具有易于区分的颜色。您可以使用基本 R colorRampPalette
函数来做到这一点。
palette <- colorRampPalette(RColorBrewer::brewer.pal(9,name = 'Set1'))(length(unique(tidied_pca$Tag)))