简体   繁体   中英

Add legend to ggplot histogram plot

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

在此处输入图片说明

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

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() .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM