如何使用线符号回收colorbrewer调色板中的颜色

时间:2013-02-27 12:42:46

标签: r ggplot2 colorbrewer

我正在使用ggplot2创建相当多的facet_wrap ped geom_line图。

虽然每个地块最多只有八行,但如果合并在一起,则有更多类似于图例的二十个类别。

与此类似: Recommend a scale colour for 13 or more categories 还有这个: In R,how do I change the color value of just one value in ggplot2's scale_fill_brewer?我想使用colorbrewer的高对比度颜色集人为地增加我可以显示的颜色数量。

一种显而易见的方法似乎是“回收”调色板中的颜色,每次使用不同的线符号。如此明亮的红色与'x'在线上可能是一个不同的类别而不是鲜红色与'o'等。

有谁能想到我会怎么做?

谢谢!

修改

这里有一些(已消毒的)数据,以及我用来制作我的情节的R代码。

数据:http://orca.casa.ucl.ac.uk/~rob/Stack%20Overflow%20question/stack%20overflow%20colours%20question%20data.csv

R代码:

csvData <- read.csv("stack overflow colours question data.csv")
p <- ggplot(csvData, 
  aes(year, percentage_of_output, colour=category, group=category)) 
p + 
  geom_line(size=1.2)  + 
  labs(title = "Can I recycle the palette colours?", y = "% of output") +
  scale_colour_brewer(palette = "Set1") + 
  theme(plot.title = element_text(size = rel(1.5))) +
  facet_wrap("country_iso3", scales="free_y")

1 个答案:

答案 0 :(得分:14)

制作包含20个级别(作为字母)的数据框。

df<-data.frame(group=rep(c(LETTERS[1:20]),each=5),x=rep(1:5,times=20),y=1:100)

您可以使用scale_colour_manual()为线条设置颜色 - 例如,我使用了五个SET1并重复了四个times(总数为20)。然后设置添加geom_point()scale_shape_manual()形状以及五种不同形状的形状,并重复each四次(总数再次为20)。

library(RColorBrewer)
ggplot(df,aes(x,y,colour=group))+geom_line()+geom_point(aes(shape=group),size=5)+
  scale_colour_manual(values=rep(brewer.pal(5,"Set1"),times=4))+
  scale_shape_manual(values=rep(c(15,16,17,18,19),each=5))

enter image description here