1

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: enter image description here

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 enter image description here

Any ideas? Thanks alot!

Leonardo Alves Machado
  • 2,747
  • 10
  • 38
  • 53
Roman_G
  • 47
  • 1
  • 5
  • The `color` aesthetic is for lines/points/borders, the `fill` aesthetic is for areas (bars, polygons, ribbons). Just change `scale_colour_manual` to `scale_fill_manual`. And delete the `fill = "red"` in your `geom_ribbon` call so it doesn't override the scale. – Gregor Thomas Oct 10 '17 at 17:41
  • Thanks alot for that easy solution! – Roman_G Oct 11 '17 at 06:59

1 Answers1

1

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

John Harley
  • 156
  • 1
  • 6