簡體   English   中英

如何在 r 中的 ggplot2 中將多個變量與 geom_smooth 一起使用

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

就像標題一樣,我想在我的 model 中使用多個自變量。

有一個簡單的例子:

如果我想查看mpgdisp之間的關系,我可以使用這個:

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

然后我想看看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 

也許我沒有在ggplot(aes())中映射hp ,我嘗試了這個但是發生了同樣的錯誤:

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

任何幫助將不勝感激!

您可以嘗試在aes()中添加colorsize

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

屈服

在此處輸入圖像描述

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