1

I plotted histogram and kde's of a vector with ggplot in R. Now I'd like to add a legend containing the three kde plots --> "rectangular", "epanechnikov" and "gaussian". I just can't make it work!

v1 = data.frame(x=rnorm(500,30,9))
ggplot(data=v1, aes(x=x)) +
  geom_histogram(binwidth=2, color="grey", fill="white") +
  geom_density(aes(y=2 * ..count..), kernel="rectangular", color="orange") + 
  geom_density(aes(y=2 * ..count..), kernel="epanechnikov", color="red") + 
  geom_density(aes(y=2 * ..count..), kernel="gaussian", color="blue") + 
  scale_color_manual(labels=c("rectangular", "epanechnikov","gaussian"),
                     values=c("orange"="orange", "red"="red", "blue"="blue"))

enter image description here

Butti
  • 25
  • 2

1 Answers1

0

You are so close. As suggested by https://stackoverflow.com/a/24497113:

ggplot(data=v1, aes(x=x)) +
  geom_histogram(binwidth=2, color="grey", fill="white") +
  geom_density(aes(y=2 * ..count.., color="rectangular"), kernel="rectangular") + 
  geom_density(aes(y=2 * ..count.., color="epanechnikov"), kernel="epanechnikov") + 
  geom_density(aes(y=2 * ..count.., color="gaussian"), kernel="gaussian") + 
  scale_color_manual(labels=c("rectangular", "epanechnikov","gaussian"),
                     values=c("rectangular"="orange", "epanechnikov"="red", "gaussian"="blue")) 

ggplot with legend

The trick is shifting color to inside aes(...), and matching them in scale_color_manual. I happened to use the same strings as kernel=, but that's not strictly necessary. Whatever you choose, though, needs to match the names of values= within scale_color_manual. (BTW: labels= is optional here, unless you want to beautify the strings some; otherwise, it'll use the color= from within each aes().)

r2evans
  • 141,215
  • 6
  • 77
  • 149