treetopdewdrop
treetopdewdrop

Reputation: 164

geom_ribbon changes plot color formatting

Appreciate help with using ggplot geom_ribbon with scale_color_manual, which is totally messing up the formatting for my plot.

Here is a 36 row sample data set:

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")

Here's what the top rows look like:

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

Using this code:

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) 

I get this plot:

enter image description here

All looks good. However, when I want to add the confidence intervals to these lines (in the same color as a transparent ribbon) by using the geom_ribbon: all the formatting gets totally mucked up.

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) 

As you can see here, the bad plot with colors all wrong: enter image description here

Can anyone help with this? And to rid this plot of those ugly borders, so the ribbon is only a transparent area with no borders?

Many thanks in advance!!

Upvotes: 0

Views: 3469

Answers (1)

www
www

Reputation: 39154

Like color, you need to use scale_fill_manual to change the color. Here is a modification of your code.

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

Upvotes: 2

Related Questions