简体   繁体   English

如何在 r 中的 ggplot2 中将多个变量与 geom_smooth 一起使用

[英]How to use multiple variables with geom_smooth in ggplot2 in r

Just like the title, I want to use multiple independent variables in my model.就像标题一样,我想在我的 model 中使用多个自变量。

There is an easy example:有一个简单的例子:

If I want to see the relationship between mpg and disp , I could use this:如果我想查看mpgdisp之间的关系,我可以使用这个:

mtcars %>% ggplot(aes(y = mpg, x = disp)) +
  geom_point() + 
  geom_smooth(formula = y ~ x)

Then I want to see the relationship between mpg and disp adjusted hp , I write the below code occurring an error:然后我想看看mpgdisp之间的关系调整了hp ,我写了下面的代码发生错误:

mtcars %>% ggplot(aes(y = mpg, x = disp)) +
  geom_point() + 
  geom_smooth(formula = y ~ x + hp)

# Computation failed in `stat_smooth()`:
# object 'hp' not found 

Maybe I didn't mapping hp in ggplot(aes()) , and I tried this but the same error occurring:也许我没有在ggplot(aes())中映射hp ,我尝试了这个但是发生了同样的错误:

mtcars %>% ggplot(aes(y = mpg, x = disp, z = hp)) +
  geom_point() + 
  geom_smooth(formula = y ~ x + z)

Any help will be highly appreciated!任何帮助将不胜感激!

You can try to add color or size in aes()您可以尝试在aes()中添加colorsize

mtcars %>% ggplot(aes(y = mpg, x = disp, color=hp)) +
  geom_point() + 
  geom_smooth(formula = y ~ x)

Yielding屈服

在此处输入图像描述

mtcars %>% ggplot(aes(y = mpg, x = disp, color=hp, size=hp)) +
  geom_point() + 
  geom_smooth(formula = y ~ x)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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