R ggplot2:添加geom_smooth()会破坏点图例

R ggplot2:添加geom_smooth()会破坏点图例,r,ggplot2,R,Ggplot2,我有一个奇怪的问题,我似乎找不到以前有类似问题的任何问题 我有以下数据: > 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> <

我有一个奇怪的问题,我似乎找不到以前有类似问题的任何问题

我有以下数据:

> 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

然而,一旦我使用geom_smooth添加了一条趋势线,传奇就会被彻底摧毁

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


我真的不确定是什么原因造成的,也不知道如何修复。我相信一定很简单。

你可以试试这个。看起来,
size
参数与您选择的形状相结合,使整个图例背景成为您选择的颜色。可以重新排列和更改图例以反映所选的灰色。这里唯一的问题是您丢失了图例中点周围的蓝色边框,但我觉得没有它您不会丢失任何信息

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")))
库(tidyverse)
df%
ggplot(aes(x=十年,y=平均值,大小=平均值,未就业))+
几何平滑(方法=lm,se=FALSE,aes(color=“lm”))+
几何点(color=“blue”,fill=“lightblue”,shape=21)+
比例尺寸面积(最大尺寸=15)+
theme_gray()+
实验室(title=“每十年的失业水平”,
副标题=“(1967-2015)”,
标题=“来自美国经济时间序列的数据。”,
tag=“图3”,
x=“失业持续时间中位数的平均值(周)”,
y=“十年”)+
辅助线(尺寸=辅助线\图例(override.aes=列表(color=“grey90”))

你可以试试这个。看起来,
size
参数与您选择的形状相结合,使整个图例背景成为您选择的颜色。可以重新排列和更改图例以反映所选的灰色。这里唯一的问题是您丢失了图例中点周围的蓝色边框,但我觉得没有它您不会丢失任何信息

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")))
库(tidyverse)
df%
ggplot(aes(x=十年,y=平均值,大小=平均值,未就业))+
几何平滑(方法=lm,se=FALSE,aes(color=“lm”))+
几何点(color=“blue”,fill=“lightblue”,shape=21)+
比例尺寸面积(最大尺寸=15)+
theme_gray()+
实验室(title=“每十年的失业水平”,
副标题=“(1967-2015)”,
标题=“来自美国经济时间序列的数据。”,
tag=“图3”,
x=“失业持续时间中位数的平均值(周)”,
y=“十年”)+
辅助线(尺寸=辅助线\图例(override.aes=列表(color=“grey90”))

我认为这是因为
size=mean.unemploy
被放在全球范围内。如果将其放入
ggplot
aes
,则会影响整个
geom
。这意味着新的
geom_smooth
也将读取
size
参数

由于
尺寸
仅用于
几何点
,因此可以将其放入
几何点
映射中。你只能改变那部分

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


如果修改前两行,则不会触及点的图例。

我认为这是因为
size=mean.unemploy
是全局放置的。如果将其放入
ggplot
aes
,则会影响整个
geom
。这意味着新的
geom_smooth
也将读取
size
参数

由于
尺寸
仅用于
几何点
,因此可以将其放入
几何点
映射中。你只能改变那部分

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


如果修改前两行,点的图例将不会被触动。

是否尝试使用
geom_smooth
中的参数
show.legend=FALSE
?这确实修复了点图例,但随后删除了趋势线图例。通过将趋势线作为一个单独的变量计算,然后使用geom_abline(),我可以得到类似的结果。我正试图具体了解geom_smooth打破点传奇的原因。也许将来我会想添加多条回归线,在这种情况下,show.legend=FALSE将不可行。尝试删除
aes(color=“lm”)
,而改用
color=“red”
。您是否尝试了
geom\u smooth
中的参数
show.legend=FALSE
?嗯,这确实解决了图例的问题,但随后趋势线图例被删除。通过将趋势线作为一个单独的变量计算,然后使用geom_abline(),我可以得到类似的结果。我正试图具体了解geom_smooth打破点传奇的原因。也许将来我会想添加多条回归线,在这种情况下,show.legend=FALSE将不可行。请尝试删除
aes(color=“lm”)
,改为使用
color=“red”
。这个答案给出了我想要的确切结果,但更重要的是,它帮助我更好地理解ggplot的工作原理。这个答案给了我想要的确切结果,但更重要的是,它帮助我更好地理解ggplot的工作原理。