gpt4 book ai didi

r - 如何在已定义的调色板中使用两次颜色?

转载 作者:行者123 更新时间:2023-12-04 12:18:48 28 4
gpt4 key购买 nike

我正在使用 Wes Anderson library of palettes创建图表。这是虚拟数据:

structure(list(Question = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L), .Label = c("One", "Two"), class = "factor"),
Agree.Disagree = structure(c(6L, 6L, 2L, 2L, 4L, 4L, 1L,
1L, 5L, 5L, 3L, 3L), .Label = c("Agree", "Disagree", "DK",
"Neither", "Strongly Agree", "Strongly Disagree"), class = "factor"),
n = c(10, 20, 50, 60, 25, 30, 45, 50, 80, 20, 15, 10), Percent = c(4,
11, 22, 32, 11, 16, 20, 26, 36, 11, 7, 5)), .Names = c("Question",
"Agree.Disagree", "n", "Percent"), row.names = c(NA, -12L), class =
"data.frame")

这是图形代码:

library(ggplot2)
library(wesanderson)

p5<-ggplot(data=df, aes(x=Question, y=n)) +
geom_bar(aes(fill = Agree.Disagree),stat="identity") +
theme_minimal() +
ggtitle("Questions about values and attitudes") +
labs(x = "",y = "n") +
scale_fill_manual(values = wes_palette("GrandBudapest2")) +
geom_text(aes(label=Percent), vjust=2, colour="white") +
coord_flip()
p5

显然这是行不通的。我曾经知道一个简单的解决方法,但令人沮丧的是不记得现在是什么了。有什么想法吗?

最佳答案

也许您可以像这样重复调色板颜色,然后它将仅使用所需数量的颜色。

scale_fill_manual(values = rep(wes_palette("GrandBudapest2"),2))

关于r - 如何在已定义的调色板中使用两次颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43801546/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com