简体   繁体   中英

How can I create a legend for confidence interval and line of best fit for a stat_smooth in a lm (ggplot2)

Hi I have a great regression graph with 95% confidence interval and best fit line, however cant seem to add a legend, I've tried aes, scale_colour_manual, in various locations but to no avail. My code so far is

ggplot(bats, aes(x=Days, y=AI))+geom_point(size=2, alpha=0.5) +
  xlab("Number of survey days") +
  ylab("Bat Activity Index")+
  stat_smooth( method = "lm",  formula = y~x,
    size = 1, col = "red", level = 0.95, alpha=0.5)+
  theme_light() +
  ggtitle("Regression test between Bat Activity Index and number of survey days")

到目前为止的图表

I should like a legend to show confidence intervals (shaded area) and line of best fit (red).

thanks in advance!

In ggplot2 , the way to create a legend is to specify your aesthetics within aes() . In this case, you have two separate aesthetics that you need to label: color should include one value that is labeled as "Line of Best Fit", and fill includes one value that is labeled as "Confidence Interval".

You don't have a column in your dataset to apply to these two aesthetics, but if you input a character string into aes(color=..., fill=...) , then the character string will be used as the label and the aesthetics will be created for you. Observe how this works below when I use a made-up dataset that matches your own formatting. Note in particular that col="red" is removed from the original code. If it was not removed, it would overwrite the color= we placed inside aes() .

library(ggplot2)

set.seed(1234)
bats <- data.frame(
  Days=sample(1:30, 200, replace=TRUE),
  AI=rnorm(200, mean=50, sd=20)
)

p <- 
  ggplot(bats, aes(x=Days, y=AI))+geom_point(size=2, alpha=0.5) +
  xlab("Number of survey days") +
  ylab("Bat Activity Index")+
  stat_smooth(
    aes(color="Line of Best Fit", fill="Confidence Interval"),
    method = "lm",  formula = y~x, size = 1,
    level = 0.95, alpha=0.5
  )+
  theme_light() +
  ggtitle("Regression test between Bat Activity Index and number of survey days") 
p

在此处输入图片说明

Well, now we have the legends, but likely this does not look like intended. The legend for color has a red line and a gray background, whereas the one for fill has a red background and a blue line! This is because ggplot2 is recognizing that "Line of Best Fit" and "Confidence Interval" are not the same string, so therefore creates different aesthetic values for both. In any case, you will want to specify the correct color and fill value.

Specifying Colors: If you specify fill= and color= in geom_smooth() , it will overwrite the one we made in aes() and you will remove the legend. Therefore, we will specify the value using scale_color_manual() and scale_fill_manual() . We also do not want titles for the legends, so can remove it there too.

Changing the Appearance of the Key Glyphs: You also want to change the way the legends look. For "Line of Best Fit", you want to remove the fill from that legend icon (called a "key glyph"). For "Confidence Interval", we want to remove the line and just show the gray box. We can do all that using guides() and the override.aes= argument within guide_legend() .

Position of Legends: Finally, We want to move the legend to the bottom for a better overall look, which is accessed via the legend.position= element in theme() . If you want to keep them on the right, you may want to modify the theme() elements for legend.spacing and legend.margin to get them a bit closer together.

Putting this all together we get the following:

p +
  scale_fill_manual(NULL, values = 'gray') +
  scale_color_manual(NULL, values = 'red') +
  guides(
    color=guide_legend(override.aes = list(fill=NA), order=1),
    fill=guide_legend(override.aes = list(color=NA), order=2)
  )+
  theme(legend.position = "bottom")

在此处输入图片说明

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