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

时间:2017-05-05 09:37:58

标签: r ggplot2

我使用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 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:2)

您可以选择第二个调色板并将它们链接起来:

NSInteger number = 301;
NSDateComponentsFormatter *formatter = [[NSDateComponentsFormatter alloc] init];
formatter.allowedUnits = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
formatter.zeroFormattingBehavior = NSDateComponentsFormatterZeroFormattingBehaviorPad;
NSString *result = [formatter stringFromTimeInterval:(NSTimeInterval)number];
NSLog(@"%@", result);