geom_ribbon更改绘图颜色格式

时间:2018-01-06 21:05:04

标签: r ggplot2

感谢使用ggplot geom_ribbon和scale_color_manual的帮助,这完全弄乱了我的情节的格式。

这是一个36行的样本数据集:

dput(areas_full_melt)
structure(list(X = 1:36, year = c(1997L, 2000L, 2003L, 2005L, 
1997L, 2000L, 2003L, 2005L, 1997L, 2000L, 2003L, 2005L, 1997L, 
2000L, 2003L, 2005L, 1997L, 2000L, 2003L, 2005L, 1997L, 2000L, 
2003L, 2005L, 1997L, 2000L, 2003L, 2005L, 1997L, 2000L, 2003L, 
2005L, 1997L, 2000L, 2003L, 2005L), group = structure(c(1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 
5L, 5L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 
9L, 9L), .Label = c("1", "2", "3", "4", "5", "6", "7", "8", "9"
), class = "factor"), max = c(272L, 235L, 236L, 238L, 4017L, 
4317L, 4453L, 4564L, 2494L, 2734L, 2867L, 2963L, 4175L, 4582L, 
4869L, 5073L, 3114L, 3320L, 3482L, 3606L, 10814L, 10715L, 10941L, 
10945L, 23010L, 22405L, 22037L, 21908L, 17921L, 17572L, 16985L, 
16607L, 7813L, 7750L, 7759L, 7726L), CI_min = c(167L, 130L, 131L, 
133L, 3442L, 3731L, 3865L, 3971L, 2095L, 2316L, 2439L, 2526L, 
3807L, 4187L, 4457L, 4648L, 2868L, 3053L, 3201L, 3314L, 10182L, 
10091L, 10310L, 10314L, 22110L, 21521L, 21160L, 21036L, 17212L, 
16875L, 16305L, 15938L, 7448L, 7389L, 7400L, 7369L), CI_max = c(377L, 
340L, 341L, 343L, 4592L, 4903L, 5041L, 5157L, 2893L, 3152L, 3295L, 
3400L, 4543L, 4977L, 5281L, 5498L, 3360L, 3587L, 3763L, 3898L, 
11446L, 11339L, 11572L, 11576L, 23910L, 23289L, 22914L, 22780L, 
18630L, 18269L, 17665L, 17276L, 8178L, 8111L, 8118L, 8083L)), .Names = c("X", 
"year", "group", "max", "CI_min", "CI_max"), row.names = c(NA, 
-36L), class = "data.frame")

这里是顶行的样子:

head(areas_full_melt)
#   X year group  max CI_min CI_max
# 1 1 1997     1  272    167    377
# 2 2 2000     1  235    130    340
# 3 3 2003     1  236    131    341
# 4 4 2005     1  238    133    343
# 5 5 1997     2 4017   3442   4592
# 6 6 2000     2 4317   3731   4903

使用此代码:

library(ggplot2)
mycolors<-c("black","#FFD1E3","#FF8782","#EE0000","#940000","#BD7F4F","#DBCF21","#4C7300","#6699CD")
ggplot(data=areas_full_melt, aes(x=year, y=max, colour=group)) +
  geom_line(aes(y = max, x = year, colour = group),data = areas_full_melt, stat="identity") +
  scale_color_manual(values=mycolors,guide = FALSE) 

我得到这个情节:

enter image description here

一切看起来都不错。但是,当我想通过使用geom_ribbon将置信区间添加到这些线条(与透明色带的颜色相同)时:所有格式都会完全混乱。

ggplot(data=areas_full_melt, aes(x=year, y=max, colour=group)) +
  geom_line(aes(y = max, x = year, colour = group),data = areas_full_melt, stat="identity") +
  geom_ribbon(aes(ymin=areas_full_melt$CI_min, ymax=areas_full_melt$CI_max,fill=group), alpha=0.6) +
 scale_color_manual(values=mycolors,guide = FALSE) 

正如你在这里看到的那样,带有颜色的错误情节都是错误的: enter image description here

任何人都可以帮忙吗?为了摆脱那些丑陋边界的情节,所以丝带只是一个没有边框的透明区域?

非常感谢提前!!

1 个答案:

答案 0 :(得分:1)

与颜色一样,您需要使用scale_fill_manual来更改颜色。以下是对代码的修改。

library(ggplot2)

# Convert the group column to factor
areas_full_melt$group <- factor(areas_full_melt$group)

# Define color palette
mycolors<-c("black","#FFD1E3","#FF8782","#EE0000","#940000","#BD7F4F","#DBCF21","#4C7300","#6699CD")

ggplot(data = areas_full_melt, aes(x = year, y = max, colour = group)) +
  geom_line() +
  geom_ribbon(aes(ymin = CI_min, ymax = CI_max, fill = group), alpha = 0.6) +
  scale_color_manual(values = mycolors, guide = FALSE) +
  scale_fill_manual(values = mycolors, guide = FALSE) # Specify color to the fill aesthetics

enter image description here