【问题标题】:Add a geom_smooth() before grouping the data in a ggplot在将数据分组到 ggplot 之前添加 geom_smooth()
【发布时间】:2020-10-28 19:55:23
【问题描述】:

我下面的 ggplot 效果很好。但是,我想知道如何添加一条回归线之前将我的datgroup_by(groups) 分组,即整个dat 上的geom_smooth()(见下图)?

library(tidyverse)

dat <- read.csv('https://raw.githubusercontent.com/rnorouzian/e/master/cw2.csv')
dat$groups <- factor(dat$groups)

dat2 <- dat %>% group_by(groups) %>% summarize(mean_x = mean(x),
                                       mean_y = mean(y),
                                       .groups = 'drop')


dat %>% group_by(groups) %>% ggplot() +  ## @@ BEFORE group_by(group) can I add a geom_smooth()
  aes(x, y, color = groups, shape = groups)+
  geom_point(size = 2) + theme_classic()+ 
  stat_ellipse(level = .6) +
  geom_point(data = dat2, 
             mapping = aes(x = mean_x, y = mean_y,fill = factor(groups)),
             size = 4, show.legend = F,shape=21) +
  geom_smooth(data = dat2, mapping = aes(x = mean_x, y = mean_y,group=1), 
              method = "lm", se=F, color = 1, formula = 'y ~ x')+
  scale_fill_manual(values=rep('black',3))

【问题讨论】:

    标签: r dataframe ggplot2 plot tidyverse


    【解决方案1】:

    试试这个:

    library(tidyverse)
    #Code
    dat %>% group_by(groups) %>% ggplot() +  ## @@ BEFORE group_by(group) can I add a geom_smooth()
      aes(x, y, color = groups, shape = groups)+
      geom_point(size = 2) + theme_classic()+ 
      geom_smooth(formula = "y~x",aes(group=1),se=F,color='black')+
      stat_ellipse(level = .6) +
      geom_point(data = dat2, 
                 mapping = aes(x = mean_x, y = mean_y,color = factor(groups)),
                 size = 4, show.legend = F) +
      geom_smooth(data = dat2, mapping = aes(x = mean_x, y = mean_y,group=1), 
                  method = "lm", se=F, color = 1, formula = 'y ~ x')
    

    输出:

    或者使用 @stefan 概念(这很棒):

    #Code 2
    dat %>% group_by(groups) %>% ggplot() +  ## @@ BEFORE group_by(group) can I add a geom_smooth()
      aes(x, y, color = groups, shape = groups)+
      geom_point(size = 2) + theme_classic()+ 
      geom_smooth(method='lm',formula = "y~x",aes(group=1),se=F,color='black')+
      stat_ellipse(level = .6) +
      geom_point(data = dat2, 
                 mapping = aes(x = mean_x, y = mean_y,color = factor(groups)),
                 size = 4, show.legend = F) +
      geom_smooth(data = dat2, mapping = aes(x = mean_x, y = mean_y,group=1), 
                  method = "lm", se=F, color = 1, formula = 'y ~ x')
    

    输出:

    【讨论】:

    • 我还有一个后续:-)
    • 嗨鸭子,HERE 是我的后续行动。有一个答案,但我想知道我们是否可以使用tidyverse 本身实现我的目标?
    【解决方案2】:

    这可以这样实现:

    顺便说一句:对数据集进行分组对绘图没有影响。 groups 是由审美决定的。

    library(tidyverse)
    
    dat <- read.csv('https://raw.githubusercontent.com/rnorouzian/e/master/cw2.csv')
    dat$groups <- factor(dat$groups)
    
    dat2 <- dat %>% group_by(groups) %>% summarize(mean_x = mean(x),
                                                   mean_y = mean(y),
                                                   .groups = 'drop')
    
    
    dat %>% 
      ggplot() +
      aes(x, y, color = groups, shape = groups)+
      geom_point(size = 2) + theme_classic()+ 
      stat_ellipse(level = .6) +
      geom_point(data = dat2, 
                 mapping = aes(x = mean_x, y = mean_y,fill = factor(groups)),
                 size = 4, show.legend = F,shape=21) +
      geom_smooth(data = dat2, mapping = aes(x = mean_x, y = mean_y,group=1), 
              method = "lm", se=F, color = 1, formula = 'y ~ x')+ 
      geom_smooth(aes(group = 1), 
                  method = "lm", se=F, color = 2, formula = 'y ~ x')+
      scale_fill_manual(values=rep('black',3))
    

    【讨论】:

    • 糟糕。对不起。您可以简单地添加您的 geom_smooth。我会解决的。
    猜你喜欢
    • 2021-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-01
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多