[英]Plotting a regression line over the conditional distribution of y for each value of x
對於 x 的每個值(在這種情況下為 educ),我想 plot y(收入)的分布並添加 y ~ x 的回歸線。
df <- structure(list(
income = c(16L, 18L, 26L, 16L, 34L, 22L, 42L,
42L, 16L, 20L, 66L, 26L, 20L, 30L, 20L, 30L, 32L, 16L, 20L, 58L,
30L, 26L, 20L, 40L, 32L, 22L, 20L, 56L, 32L, 30L, 30L, 48L, 40L,
84L, 50L, 38L, 30L, 76L, 48L, 36L, 40L, 44L, 30L, 60L, 24L, 88L,
46L, 50L, 50L, 22L, 26L, 46L, 22L, 24L, 64L, 62L, 24L, 50L, 32L,
34L, 52L, 24L, 22L, 20L, 30L, 24L, 120L, 22L, 82L, 18L, 26L,
104L, 28L, 32L, 38L, 44L, 22L, 18L, 24L, 56L),
educ = c(10L, 7L, 9L, 11L, 14L, 12L, 16L, 16L, 9L, 10L, 16L, 12L, 10L, 15L,
10L, 19L, 16L, 11L, 10L, 16L, 12L, 10L, 8L, 12L, 10L, 11L, 10L,
14L, 12L, 11L, 14L, 14L, 7L, 18L, 10L, 12L, 12L, 16L, 16L, 11L,
11L, 12L, 10L, 15L, 9L, 17L, 16L, 16L, 14L, 11L, 12L, 16L, 9L,
9L, 14L, 16L, 10L, 13L, 10L, 16L, 18L, 12L, 14L, 13L, 14L, 13L,
18L, 10L, 16L, 12L, 12L, 14L, 12L, 12L, 14L, 12L, 12L, 10L, 12L,
20L),
race = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3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L, 0L, 0L, 0L)), row.names = c(NA, -80L), class = c("tbl_df",
"tbl", "data.frame"))
到目前為止,我已經使用ggridges
package 到 plot y 在每個 x 值處的分布。 盡管如此,通過這樣做,我實際上必須更改每個變量的坐標(x 變為 y,反之亦然)。 為了“還原”這個,我翻轉了坐標,結果我得到了這個:
ggplot(df, aes(x = income, y = educ, group = educ)) +
geom_density_ridges(jittered_points = TRUE,
position = position_points_jitter(height = 0),
point_size = 1.5,
point_shape = 1,
alpha = 0.3) +
coord_flip()
問題是,如果我向 plot 添加一條回歸線,我會得到每個 educyr 值的回歸線(因為我必須將它們分組以應用geom_density_ridges()
)。 此外,回歸線實際上是 x ~ y 而不是 y ~ x。
為了解決這個問題,我發現 x ~ y 的回歸線等同於 y ~ x,因此回歸線看起來與我應用geom_smooth()
相同,但 educyr 為 x,hrinc 為 y。
fit <- lm(df$income ~ df$educ)
slope <- 1/fit$coefficients[[2]]
intercept <- fit$coefficients[[1]]/fit$coefficients[[2]] * -1
ggplot(df, aes(x = income, y = educ, group = educ)) +
geom_density_ridges(jittered_points = TRUE,
position = position_points_jitter(height = 0),
point_size = 1.5,
point_shape = 1,
alpha = 0.3) +
stat_function(fun=function(x) intercept + slope*x, color = "red") +
scale_y_continuous(breaks=seq(0, 20, 5), limits=c(8, 20)) +
coord_flip()
如果我使用過,這與我會得到的相同:
ggplot(df, aes(x = educ, y = income)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE)
我想知道是否有更好的方法來做到這一點。 具體來說,如果有辦法 plot 使用ggplot2
但不使用ggridges
的每個 x 值的 y 分布,所以我不需要反轉坐標。
聽起來好像您想代表educ
的每個(分箱)值的一維income
密度。 我認為ggridges
方法在這里很好。 如果您想要另一種方法,您可以使用geom_tile
來完成,其中填充或 alpha 表示密度。 不過,這需要先手動構建密度,這有點麻煩。 最終結果非常好,但我不相信它比ggridges
更好。 但是,它確實具有不需要翻轉以進行回歸的好處:
d <- do.call(c, lapply(split(df$income, round(df$educ)), function(x) {
if(length(x) > 1)
density(x, from = 12, to = 125)$y * length(x)
else
numeric(512)}))
df_dens <- data.frame(educ = rep(sort(unique(round(df$educ))), each = 512),
income = rep(seq(12, 125, length.out = 512),
length(sort(unique(round(df$educ))))),
dens = d)
ggplot(df, aes(x = educ, y = income)) +
geom_tile(data = df_dens, aes(alpha = dens), fill = "red") +
scale_alpha_continuous(range = c(0, 1)) +
geom_point() +
geom_smooth(method = "lm", colour = "red4", se = FALSE, linetype = 2)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.