2017-05-05 59 views
2

我正在使用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 

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

回答

2

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

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

没错,我觉得这是我以前那样。 –

2

您可以选择第二个调色板,并把它们连:

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 = c(wes_palette("GrandBudapest2"), wes_palette("Moonrise3"))) + 
    geom_text(aes(label=Percent), vjust = 2, colour="white") + 
    coord_flip() 
+1

非常酷,我不知道这是可能的。我真的很喜欢它允许的变化(并且制作一个非常漂亮的图)。 –

相关问题