简体   繁体   中英

Format legend for multiple layers ggplot2

I'm trying to modify the legend in my graph. The graph it self is so far ok, but 2 points were missing. The Legend for the stat_density() layer - which is the over all density -, and the legend for the to geom_vline() layers - which is the means of the variables. I tried with guides = TRUE, but the result is not what I want.

How can I add theses? And how can I modify these? Oh and how I can get rid of the diagonal lines if I set colour in geom_density()?

Thanks for your help!

Here is my code for the graph:

ggplot(DEMDes) +
  geom_density(alpha=.3, colour=NA, aes(x=AGE, fill=factor(SEX))) +
  stat_density(geom = "line", aes(x=AGE), colour = "black", linetype="dashed") +
  geom_vline(data=cdf[1,], aes(xintercept=rating.mean), colour="#122bc7",
             linetype="dashed", size=1) +
  geom_vline(data=cdf[2,], aes(xintercept=rating.mean) , colour="#dc143c",
             linetype="dashed", size=1) +
  scale_fill_manual(name = "Geschlecht", values = c("#122bc7", "#dc143c"), labels = c("1" = "Männlich", "2" = "Weiblich"))

在此处输入图片说明


UPDATE

Here the first 500 rows of the data frame:

structure(list(SEX = c(2, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 2, 
2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 1, 1, 1, 1, 1, 2, 2, 2, 1, 
1, 1, 2, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 1, 1, 2, 2, 1, 2, 1, 1, 
2, 2, 2, 1, 1, 2, 1, 2, 2, 2, 2, 1, 2, 1, 2, 2, 1, 2, 1, 2, 2, 
2, 2, 2, 1, 1, 2, 1, 2, 2, 1, 2, 1, 2, 2, 1, 1, 1, 2, 1, 1, 2, 
1, 2, 2, 2, 1, 2, 2, 1, 2, 2, 1, 2, 1, 2, 1, 1, 2, 2, 1, 2, 1, 
2, 1, 2, 1, 2, 2, 1, 2, 1, 2, 2, 2, 2, 1, 1, 1, 1, 2, 1, 2, 2, 
1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 1, 2, 1, 2, 1, 1, 
2, 1, 2, 1, 2, 1, 2, 1, 1, 2,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19, 56, 58, 26, 79, 71, 
85, 79, 75, 67, 65, 97, 74, 66, 62, 46, 20, 22, 50, 41, 44, 41, 
18, 54, 60, 66, 68, 80, 45, 59, 62, 62, 54, 67, 75, 75, 44, 78, 
68, 67, 55, 54, 56, 62, 69, 70, 68, 52, 48, 23, 74, 82, 57, 40, 
41, 58, 53, 62, 64, 85, 85, 52, 34, 36, 77, 83, 36, 49, 46, 79, 
76, 63, 43, 44, 18, 18, 36, 41, 75, 66, 78, 65, 61, 61, 59, 21, 
61, 67, 57, 55, 57, 47, 41, 49, 65, 84, 54, 69, 60, 81, 62, 60, 
76, 77, 66, 79, 74, 67, 64, 66, 76, 50, 49, 55, 54, 59, 54, 63, 
48, 81, 54, 64, 43, 21, 64, 85, 79, 67, 55, 60, 59, 55, 83, 80, 
79, 47, 37, 62, 59, 81, 80, 65, 65, 76, 75, 73, 73, 83, 45, 47, 
65, 63, 66, 63, 58, 84, 72, 72, 42, 47, 65, 47, 42, 80, 54, 53, 
87, 63, 64, 54, 50, 61, 66, 57, 63, 62, 41, 73, 70, 68, 73, 69, 
55, 79, 52, 63, 66, 66, 47, 63, 62, 73, 46, 89, 83, 80, 64, 58, 
89, 40, 51, 46, 64, 64, 55, 76, 52, 64, 66, 60, 59, 73, 67, 66, 
64, 47, 39, 51, 46, 42, 18, 73, 46, 40, 24, 42, 53, 18, 49, 45, 
22, 79, 65, 69, 69, 74, 69, 72, 53, 26, 78, 75, 50, 52, 69, 69, 
65, 59, 75, 77, 78, 81, 83, 62, 67, 37, 68, 81, 84, 61, 56, 25, 
61, 55, 54, 28, 54, 47, 64, 63, 49, 53, 50, 61, 68, 67, 72, 51, 
21, 55, 51, 22, 62, 61, 55, 31, 54, 63, 59, 34, 74, 64, 73, 79, 
76, 21, 60, 59, 60, 82, 76, 71, 66, 61, 19, 55, 54, 54, 27, 57, 
67, 49, 83, 76, 72, 72, 45, 42, 64, 62, 53, 49, 21, 18, 65, 82, 
31, 62, 62, 28, 25, 60, 57, 72, 70, 66, 64, 48, 22, 41, 67, 64, 
66, 60, 70, 68, 65, 67, 64, 65, 60, 58, 38, 73, 68, 79, 67, 68, 
85, 68, 64, 69, 40, 19, 37, 54, 89, 84, 95, 70, 76, 74, 92, 54, 
54, 73, 74, 46, 48, 92, 87, 49, 49, 20, 66, 79, 67, 69, 80, 78, 
57, 56, 63, 49, 45, 92, 44, 46, 47, 46, 58, 54, 18, 44, 41, 60, 
54, 69, 45, 81, 80, 60, 75, 71, 73, 71, 71, 70, 48, 42, 81, 83, 
48, 43, 20, 63, 62, 73, 47, 63, 44, 34, 19, 18, 87, 82, 48, 75, 
63, 80, 51, 76, 50, 51, 79, 40, 75, 83, 82, 66, 64, 58, 56, 22, 
19, 71, 43, 39, 45, 18, 60, 21, 19, 70, 68, 33, 58, 49, 48, 65, 
66, 44, 55, 55, 55, 33, 46, 63, 72, 48, 46, 55, 53, 22, 20, 60, 
57, 64, 60, 29, 56, 57, 78, 43, 56, 52)), .Names = c("SEX", "AGE"
), row.names = c(NA, 500L), class = "data.frame")

The result might look a bit different of course. The cdf is just the mean of the SEX...

cdf <- ddply(DEMDes, .(SEX), summarise, rating.mean=mean(AGE))

A possible but probably not elegant solution is:

ggplot(DEMDes) +
  geom_density(alpha=.3, colour=NA, aes(x=AGE, fill=factor(SEX))) +
  stat_density(geom = "path", aes(x=AGE, colour = "black"), linetype="dashed", position = "identity") +
  geom_vline(data=cdf[1,], aes(xintercept=rating.mean, colour="#122bc7"),
             linetype="dashed", size=1) +
  geom_vline(data=cdf[2,], aes(xintercept=rating.mean, colour="#dc143c"),
             linetype="dashed", size=1) +
  scale_fill_manual(name = "Geschlecht", values = c("#122bc7", "#dc143c"), labels = c("1" = "Männlich", "2" = "Weiblich")) +
  scale_color_manual(name="", values =c("#122bc7", "#dc143c", "black"), labels = c("#122bc7" = expression(paste("Männlich ", bar(x) == "49.46")), "#dc143c" = expression(paste("Weiblich ", bar(x) == "49.38")), "black"= "Beide Geschlechter"))

在此处输入图片说明

I'm no whiz at ggplot, but would this work?

+ scale_colour_manual("", 
                  breaks = c("Mean", "Density"),
                  values = c("red", "black"),
                  linetype=c("dashed","dashed") 

I cannot speak to the diagonal lines question, actually you may want to clarify that a bit more as it is unclear what you are asking.

Without a reproducible dataset I can only offer a partial answer. This great resource suggests a minor hack to get rid of diagonal slashes through modified legend colors. For example:

# A hack to hide the slashes: first graph the bars with no outline and add the legend,
# then graph the bars again with outline, but with a blank legend.
ggplot(data=PlantGrowth, aes(x=group, fill=group)) + geom_bar() + geom_bar(colour="black", show_guide=FALSE) 

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