简体   繁体   中英

ggplot2: Adding geom_smooth() destroys the point legend

I have a strange issue and I can't seem to find any previous questions with a similar problem.

I have the data:

> econ3
# A tibble: 6 x 6
# Groups:   decade [6]
  decade mean.pce mean.pop mean.uempmed mean.unemploy mean.psavert
   <dbl>    <dbl>    <dbl>        <dbl>         <dbl>        <dbl>
1   1960     568.  201165.         4.52         2854.        11.2 
2   1970    1038.  214969.         6.29         5818.        11.8 
3   1980    2620.  237423.         7.2          8308.         9.30
4   1990    4924.  264777.         7.58         7566.         6.71
5   2000    8501.  294869.         9.26         8269.         4.26
6   2010   11143.  314800.        18.2         12186.         5.7

When I use this to make a plot, everything looks great:

ggplot(econ3, aes(x=decade, y=mean.uempmed, size=mean.unemploy),guide=FALSE)+
  geom_point(colour="blue", fill="lightblue", shape=21)+ 
  scale_size_area(max_size = 15)+
  theme_gray()+
  labs(title = "Unemployment Level per Decade",
       subtitle = "(1967-2015)",
       caption = "Data from the US economic time series.",
       tag = "Figure 3",
       x = "Mean of Median Duration of Unemployment (Weeks)",
       y = "Decade")

Plot as expected

However, as soon as I add a trendline using geom_smooth, the legend gets completely destroyed.

ggplot(econ3, aes(x=decade, y=mean.uempmed, size=mean.unemploy),guide=FALSE)+
  geom_point(colour="blue", fill="lightblue", shape=21)+ 
  scale_size_area(max_size = 15)+
  geom_smooth(method=lm, se=FALSE, formula = y~x, aes(color="lm"))+
  theme_gray()+
  labs(title = "Unemployment Level per Decade",
       subtitle = "(1967-2015)",
       caption = "Data from the US economic time series.",
       tag = "Figure 3",
       x = "Mean of Median Duration of Unemployment (Weeks)",
       y = "Decade")

Plot with trendline and broken legend

I'm not really sure what is causing this or how to fix it. I'm sure it must be something simple.

I think it is because size=mean.unemploy is placed globally. If you put it in the aes of ggplot , it would affect whole geom . It means that the new geom_smooth would read the size argument, either.

Since the size is only needed for the geom_point , it is okay to put it in mapping of _point . You might only change that part.

library(tidyverse)
# your dataset
ggplot(econ3, aes(x=decade, y=mean.uempmed),guide=FALSE) + # remove size aesthetic
  geom_point(aes(size=mean.unemploy), colour="blue", fill="lightblue", shape=21) + # size aesthetic in geom_point
  scale_size_area(max_size = 15)+
  geom_smooth(method=lm, se=FALSE, formula = y~x, aes(color="lm"))+
  theme_gray()+
  labs(title = "Unemployment Level per Decade",
       subtitle = "(1967-2015)",
       caption = "Data from the US economic time series.",
       tag = "Figure 3",
       x = "Mean of Median Duration of Unemployment (Weeks)",
       y = "Decade")

在此处输入图片说明

If you modify the first two lines , the legend of the points would not be touched.

You could try this out. It looks like the size argument in combination with the shape you chose makes the entire legend background the color you chose. You can rearrange and change the legend to reflect the gray color you chose. The only issue here is that you lose the blue border around the points in the legend, but I feel like you do not lose any information without it.

library(tidyverse)

df <- read_table2("decade mean.pce mean.pop mean.uempmed mean.unemploy mean.psavert
   1960     568.  201165.         4.52         2854.        11.2 
   1970    1038.  214969.         6.29         5818.        11.8 
   1980    2620.  237423.         7.2          8308.         9.30
   1990    4924.  264777.         7.58         7566.         6.71
   2000    8501.  294869.         9.26         8269.         4.26
   2010   11143.  314800.        18.2         12186.         5.7")


df %>%
  ggplot(aes(x=decade, y=mean.uempmed, size=mean.unemploy))+
  geom_smooth(method=lm, se=FALSE, aes(colour = "lm"))+
  geom_point(colour="blue", fill="lightblue", shape=21)+ 
  scale_size_area(max_size = 15)+
  theme_gray()+
  labs(title = "Unemployment Level per Decade",
       subtitle = "(1967-2015)",
       caption = "Data from the US economic time series.",
       tag = "Figure 3",
       x = "Mean of Median Duration of Unemployment (Weeks)",
       y = "Decade")+
  guides(size = guide_legend(override.aes = list(color = "grey90")))

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