1

The legend for the following plot is not showing. I would like 2 legend entries. One for purple and one for green.

library(ggplot2)

index <- c(1,2,3,4,5)
size <- c(10,20,25,40,45)
df <- data.frame(index, size)

p <- ggplot(df, aes(x=index, y=size, fill=size)) 

p +
  geom_ribbon(aes(fill="dfg", ymin=size, ymax=df$size[nrow(df)]), fill="#BEAED4", alpha=0.2, color="#BEAED4") +
  geom_area(aes(fill="abc"), fill="#84CA72", alpha=0.2, colour="#84CA72") +
  scale_fill_manual(name="skdjf", breaks=c("abc", "dfg"), values=c("red", "blue"), labels = c("cat1", "cat2"))

enter image description here

PS: I tried a lot of stuff but can't seem to get it to work.

eclipse
  • 2,831
  • 3
  • 26
  • 34

1 Answers1

3

You are assigning fill within and outside of the aesthetic

Try this

library(tidyverse)

data.frame(index = c(1,2,3,4,5), size = c(10,20,25,40,45)) %>% 
ggplot(aes(index, size)) + 
  geom_ribbon(aes(ymin = min(size), ymax = max(size), fill = 'dfg', color = 'dfg'), alpha = 0.2) + 
  geom_area(aes(fill = 'abc', color = 'abc'), alpha = 0.2) +
  scale_fill_manual(name = 'a name', values = c("#BEAED4","#84CA72")) +
  scale_color_manual(name = 'a name', values = c("#BEAED4","#84CA72"))
B Williams
  • 1,992
  • 12
  • 19