基于分类数据的geom_ribbon填充的自定义颜色 [英] Custom colours for geom_ribbon fill based for categorical data

查看:72
本文介绍了基于分类数据的geom_ribbon填充的自定义颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用scale_colour_manual中定义的自定义颜色来填充ggplot2中的geom_ribbon.这是我从

很显然,没有使用为分类变量类别"定义的颜色.而是使用默认调色板(scale_colour_hue?).我可以将fill参数放在es之外:

  ggplot(数据,aes(数字,值,颜色=类别,填充=类别))+scale_colour_manual(values = c("A" =黑色","B" =红色","C" =洋红色","D" =绿色"))+geom_smooth(method ="loess",se = FALSE)+geom_ribbon(aes(x = num,ymax = upperLoess,ymin = lowerLoess),fill =红色",alpha = 0.2) 

这会导致红丝带

有什么想法吗?非常感谢!

解决方案

尝试使用在scale_color_manual参数中定义的相同颜色添加scale_fill_manual.

  ggplot(data,aes(num,value,colour = category,fill = category))+scale_colour_manual(values = c("A" =黑色","B" =红色","C" =洋红色","D" =绿色"))+geom_smooth(method ="loess",se = FALSE)+geom_ribbon(aes(x = num,ymax = upperLoess,ymin = lowerLoess,fill = category),alpha = 0.2)+scale_fill_manual(值= c("A" =黑色","B" =红色","C" =洋红色","D" =绿色")) 

匹配颜色

I'm trying to use custom colours defined in scale_colour_manual to fill a geom_ribbon in ggplot2. Here is an example I took from Custom ggplot2 shaded error areas on categorical line plot:

set.seed(12345)
data <- cbind(rep("A", 100), rnorm(100, 0, 1))
data <- rbind(data, cbind(rep("B", 100), rnorm(100, 5, 1)))
data <- rbind(data, cbind(rep("C", 100), rnorm(100, 10, 1)))
data <- rbind(data, cbind(rep("D", 100), rnorm(100, 15, 1)))
data <- cbind(rep(1:100, 4), data)
data <- data.frame(data)
names(data) <- c("num", "category", "value")
data$num <- as.numeric(data$num)
data$value <- as.numeric(data$value)
data$upper <- data$value+10
data$lower <- data$value-10

data = data[order(data$category, data$num),]

data$upperLoess = unlist(lapply(LETTERS[1:4], 
                                function(x) predict(loess(data$upper[data$category==x] ~ 
                                                            data$num[data$category==x]))))
data$lowerLoess = unlist(lapply(LETTERS[1:4], 
                                function(x) predict(loess(data$lower[data$category==x] ~ 
                                                            data$num[data$category==x]))))

ggplot(data, aes(num, value, colour=category, fill=category)) +
  scale_colour_manual(values = c("A"="black", "B"="red", "C"="magenta", "D"="green")) +
  geom_smooth(method="loess", se=FALSE) +
  geom_ribbon(aes(x=num, ymax=upperLoess, ymin=lowerLoess, fill=category),
              alpha=0.2)

Wrong color ribbons:

Obviously, the colours defined for the categorical variable "category" are not used. Instead, the default palette (scale_colour_hue?) is used. I can place the fill argument outside the aes:

    ggplot(data, aes(num, value, colour=category, fill=category)) +
  scale_colour_manual(values = c("A"="black", "B"="red", "C"="magenta", "D"="green")) +
  geom_smooth(method="loess", se=FALSE) +
  geom_ribbon(aes(x=num, ymax=upperLoess, ymin=lowerLoess), fill="red", 
              alpha=0.2)

which results in red ribbons

Any ideas? Thanks alot!

解决方案

Try adding scale_fill_manual using the same colors defined in you scale_color_manual argument.

ggplot(data, aes(num, value, colour=category, fill=category)) + 
    scale_colour_manual(values = c("A"="black", "B"="red", "C"="magenta", "D"="green")) +
    geom_smooth(method="loess", se=FALSE) +
    geom_ribbon(aes(x=num, ymax=upperLoess, ymin=lowerLoess, fill=category),
          alpha=0.2) +
    scale_fill_manual(values = c("A"="black", "B"="red", "C"="magenta", "D"="green")) 

matching colors

这篇关于基于分类数据的geom_ribbon填充的自定义颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆