如何解决ggplot2 改变条形的颜色错误 n 太大,调色板允许的最大值
我有以下基于此 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()
但是对于实际的错误消息,没有具有这么多不同颜色的调色板。对于 Set1
,您获得的最大颜色是 9。您可以在这些值之间进行插值以获得更多颜色,但是这样您可能会失去具有易于区分颜色的良好特性。您可以使用基本的 R colorRampPalette
函数来做到这一点。
palette <- colorRampPalette(RColorBrewer::brewer.pal(9,name = 'Set1'))(length(unique(tidied_pca$Tag)))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。