如何在ggplot2中向散点图添加平滑回归线

问题描述 投票:0回答:1

我尝试使用 R 中的以下代码向下面的散点图添加平滑回归线:

lasso.model.bin.df.nozero %>%
  mutate(variable = fct_reorder(variable, lasso.coef)) %>%
  ggplot(aes(lasso.coef, variable, color = color)) +
  geom_vline(xintercept = 0, linetype = 2) +
  geom_point(size = 2) +
  geom_smooth(method = "lm", se = TRUE) +
  scale_color_identity() +
  scale_x_continuous(breaks=c(-3, 0, 3, 6), 
                      limits=c(-3,6)) +
  xlab(paste("Aggregate binomial regression estimate",
             "(14th CE-18th CE)", sep = "\n")) +
  theme_bw(base_size = 16) +
  theme(panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.major.y = element_line(linetype = 2))

结果图如下。可以看到,线路没有添加成功。

如何添加更平滑的回归线,以便更好地描绘绘图的趋势?可重现的数据如下:

lasso.model.bin.df.nozero <- structure(list(verb = c("ThemeDef.bin", "ThemeAnimacy.bin", 
"LenDiff", "chuanshou", "ci", "dai", "di", "fa.1", "fen", "fu", "gei", "huan.1", 
"ji", "jia", "jiang.1", "jiao.1", "juan", "ke", "liu", "mai.1", 
"mai.2", "pei", "qi", "rang", "shang", "shao", "she", "shu", 
"song", "ti", "tiao", "xian", "xie", "yao", "zhao"), lasso.coef = c(2.42343012998206, 
1.50992004346556, -0.260489747921125, -0.437974835187702, -1.43186775058091, 
2.45953289423522, -1.00332755937803, -1.76710253151868, 0.0159421672397665, 
-1.56095215723717, -2.40012976566433, 4.28392746224767, -2.96910403951047, 
-1.96349687513487, 5.83503207861952, -1.654161123018, 2.47749182388819, 
4.02351393169698, 1.66385027569787, 1.53630222452288, -0.5057862134867, 
5.1687920938273, 3.76269176532588, -1.76637023953311, -0.506162662311249, 
0.10462960268202, -1.90739588803801, -2.25102305327462, 1.05244125756794, 
-0.716005133024394, -0.97649586552789, -2.52457183130426, -0.200876155316301, 
1.11320557096669, 3.74031197421905), epoch.estimate.num = c(0.00158580281442652, 
0.000636692078700501, 0.000108462531421402, 9.08252501762538e-05, 
3.36193443720819e-05, 0.00164400486304142, 5.16052716573677e-05, 
2.40437820754568e-05, 0.00014299392691628, 2.9548154585438e-05, 
1.27669584768092e-05, 0.0101048842420082, 7.22748941329972e-06, 
1.97565753538538e-05, 0.0459362695115951, 2.69185315665559e-05, 
0.00167374627671508, 0.00780625059412536, 0.00074256501712568, 
0.000653701814338089, 8.48709535307841e-05, 0.0241337216285987, 
0.00602487292456066, 2.40613951688994e-05, 8.48390126830835e-05, 
0.000156252975971102, 2.08965946920693e-05, 1.48198148117073e-05, 
0.000403042143666465, 6.87811056923123e-05, 5.30085977250099e-05, 
1.12731060412884e-05, 0.000115124219042401, 0.000428281288183683, 
0.00589232079734365), rank = 1:35, color = c("forestgreen", "forestgreen", 
"red", "red", "red", "forestgreen", "red", "red", "forestgreen", 
"red", "red", "forestgreen", "red", "red", "forestgreen", "red", 
"forestgreen", "forestgreen", "forestgreen", "forestgreen", "red", 
"forestgreen", "forestgreen", "red", "red", "forestgreen", "red", 
"red", "forestgreen", "red", "red", "red", "red", "forestgreen", 
"forestgreen")), row.names = c(NA, -35L), class = "data.frame")
r ggplot2 visualization scatter-plot
1个回答
0
投票

如果您想要一条回归线:

+ geom_smooth(method = "lm", aes(y = as.numeric(variable), colour = NA))

如果您想按颜色分离回归线:

+ geom_smooth(method = "lm", aes(y = as.numeric(variable)))
© www.soinside.com 2019 - 2024. All rights reserved.