【问题标题】:How to keep single regression line and color dot in ggscatter by group如何按组在ggscatter中保留单个回归线和颜色点
【发布时间】:2022-12-19 16:50:11
【问题描述】:

我有以下代码:

library(ggpubr)
df <- mtcars
ggscatter(df, x = "wt", y = "mpg",
          add = "reg.line",                         # Add regression line
          conf.int = TRUE,                          # Add confidence interval
          color = "cyl", palette = "jco",           # Color by groups "cyl"
          shape = "cyl"                             # Change point shape by groups "cyl"
          )+
  stat_cor(aes(color = cyl), label.x = 3)           # Add correlation coefficient

产生以下情节:

在该图中,cyl 的每个成员都有 3 条回归线。

我想用 cyl 给点上色,但只有一条回归线。 我怎样才能做到这一点?

【问题讨论】:

    标签: r plot regression ggpubr


    【解决方案1】:

    一种选择是使用 geom_smooth 手动添加回归线:

    library(ggpubr)
    #> Loading required package: ggplot2
    df <- mtcars
    df$cyl <- factor(df$cyl)
    ggscatter(df,
      x = "wt", y = "mpg",
      color = "cyl", palette = "jco", # Color by groups "cyl"
      shape = "cyl" # Change point shape by groups "cyl"
    ) +
      geom_smooth(aes(group = 1), method = "lm", color = "black") +
      stat_cor(aes(group = 1), label.x = 3)
    #> `geom_smooth()` using formula = 'y ~ x'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-24
      • 1970-01-01
      • 2022-01-07
      • 1970-01-01
      • 2020-03-18
      • 1970-01-01
      • 2018-02-13
      • 1970-01-01
      相关资源
      最近更新 更多