[英]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.