我喜欢默认的ggplot2颜色,但是对于我正在使用的数据集,读者可以分辨出两个相邻类别之间的差异非常重要。使用默认颜色很困难,因为我有10个类别,因此生成的颜色在相邻因子组之间非常相似。
以下是一个例子:
library(ggplot2)
x=rnorm(100,1,2)
y=rnorm(100,1,2)
category=letters[1:10]
data=cbind.data.frame(x,y,category)
ggplot(data,aes(x,y,colour=category))+stat_smooth(alpha=0)
给出这样的东西:
例如,很难计算出这个图上的d
和e
,因为它们都是绿色的,并且区分相邻的因子组很重要我的数据。现有的订单也很重要,我希望它在图例中按顺序出现。
所以我的问题是;有没有办法保持可爱的默认ggplot2颜色(跨10个因素级别),但将顺序混合一点,以便相邻的类别看起来更加可以区分?在我的数据集中,非相邻类别通常可以通过它们的实际分布来确定,因此颜色对于那些不太重要。
我用不同的scale_colour_brewer()
调色板搞砸了,但颜色不是很漂亮。
答案 0 :(得分:3)
要改变颜色的顺序,可以使用
require(scales)
n <- length(levels(data$category)) # number of colors
cols <- hue_pal(h = c(0, 360) + 15,
c = 100, l = 65,
h.start = 0, direction = 1)(n)[order(sample(1:n, n))] # color palette in random order
ggplot(data,aes(x,y,colour=category))+stat_smooth(alpha=0, size=2) +
scale_color_manual(values = cols)
如果您想要可辨别的颜色,请查看this post。
答案 1 :(得分:1)
library(ggplot2)
library(scales)
set.seed(42)
x <- rnorm(100,1,2)
y <- rnorm(100,1,2)
category <- letters[1:10]
data <- cbind.data.frame(x,y,category)
cols <- sample(hue_pal()(10))
ggplot(data,aes(x,y,colour=category)) + stat_smooth(alpha=0) + scale_colour_manual(values=cols)