簡體   English   中英

使用 ggplot2 的多重回歸線

[英]Multiple regression lines using ggplot2

我需要在使用 ggplot2 制作的散點圖上創建多個回歸線,該散點圖有兩個值,男性和女性,但是我在嘗試設置代碼時遇到了一些麻煩。 我使用以下代碼創建了一個顯示兩組數據的散點圖:

ggplot(LifeSatisfaction, aes(x=Country)) + 
  geom_point(aes(y = Life_Satisfaction_Female), color = "palevioletred2") + 
  geom_smooth(method = "lm", y = Life_Satisfaction_Female, col = "red") +
  geom_point(aes(y = Life_Satisfaction_Male), color="steelblue") +
  geom_smooth(method = "lm", y = Life_Satisfaction_Male, col = "blue") +
  labs (title = "Life Satisfaction per Country", x = "Country", y = "Life Satisfaction Rating") + ylim(5, 8)

我嘗試使用geom_smooth () ,但它似乎不適用於多個值。 任何和所有的幫助表示贊賞!

編輯:只是想說我對 rstudio 和一般編碼很陌生,所以請簡單解釋一下哈哈

geom_point中, y美學映射到Life_Satisfaction列,因此它從該列獲取其值。 如果您對geom_smooth執行相同的操作(將y=Life_Satisfaction_X放入aes() ),它應該正確地從該列獲取數據:

ggplot(LifeSatisfaction, aes(x=Country)) + 
  geom_point(aes(y = Life_Satisfaction_Female), color = "palevioletred2") + 
  geom_point(aes(y = Life_Satisfaction_Male), color = "steelblue") +
  geom_smooth(aes(y = Life_Satisfaction_Female), method = "lm", col = "red") +
  geom_smooth(aes(y = Life_Satisfaction_Male), method = "lm", col = "blue") +
  labs(title = "Life Satisfaction per Country", x = "Country", y = "Life Satisfaction Rating") +
  ylim(5, 8)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM