有限(x)中的误差;需要添加一个额外的线到一个折线图(dplyr)

vnzz0bqm  于 2023-02-14  发布在  其他
关注(0)|答案(1)|浏览(65)

我已经尝试搜索此问题,但没有结果。已设法重现以下错误。
问题是:我尝试添加第四行,表示每年所有字母的平均值。到目前为止,我只能生成每个字母的平均值。在最后一行geom_line之前,一切都运行良好(),这意味着生成聚合错误。()。另一个考虑是在Letters下添加"Mean"值,以便无论如何都能生成它们,但我相信有一个更简单的方法。

library(tidyverse)

Letters <- rep(c("A","B","C"),20)
Years <- rep(c(1990:1999),6)
Numbers <- runif(60, min = 0, max = 20) 

df <- data.frame(Letters, Years, Numbers) %>% 
        group_by(Letters,Years) %>% 
        summarise(Letter_Mean= mean(Numbers),.groups = 'drop')

meanallletters <- df %>% 
        group_by(Years) %>% 
        summarise(all_mean = mean(Numbers),.groups =  'drop') %>% 
        select(-Years)

lineplotsample <- df %>% 
        ggplot(aes(x=Years, y=Letter_Mean, color = Letters))

## this doesn't work
lineplotsample + geom_line() + geom_point() + geom_line(aes(Years, y= meanallletters))

## this works, but missing the line representing aggregate mean
lineplotsample + geom_line() + geom_point()
t9aqgxwy

t9aqgxwy1#

我将汇总数据,然后将其绑定到原始数据的底部,如下所示:

library(tidyverse)

Letters <- rep(c("A","B","C"),20)
Years <- rep(c(1990:1999),6)
Numbers <- runif(60, min = 0, max = 20) 

df <- data.frame(Letters, Years, Numbers) %>% 
  group_by(Letters,Years) %>% 
  summarise(Letter_Mean= mean(Numbers),.groups = 'drop')

meanallletters <- df %>% 
  group_by(Years) %>% 
  summarise(Letters = "All", 
            Letter_Mean = mean(Letter_Mean))  %>% 
  bind_rows(df,.) %>% 
  ungroup %>% 
  mutate(Letters = factor(Letters, levels=c("A", "B", "C", "All")))

meanallletters %>% 
  ggplot(aes(x=Years, y=Letter_Mean, color = Letters)) + 
  geom_line() + 
  geom_point()

reprex package(v2.0.1)于2023年2月12日创建
这里有一个更通用的指定级别的方法,它也处理了Letters最初是一个因素的情况。

library(tidyverse)

Letters <- rep(LETTERS,20)
Years <- rep(c(1990:1999),26)
Numbers <- runif(26*10, min = 0, max = 20) 

df <- data.frame(Letters, Years, Numbers) %>% 
  group_by(Letters,Years) %>% 
  summarise(Letter_Mean= mean(Numbers),.groups = 'drop')

meanallletters <- df %>% 
  mutate(Letters = as.character(Letters)) %>% 
  group_by(Years) %>% 
  summarise(Letters = "All", 
            Letter_Mean = mean(Letter_Mean))  %>% 
  bind_rows(df,.) %>% 
  ungroup %>% 
  mutate(Letters = factor(Letters, levels=c(levels(as.factor(df$Letters)), "All")))

meanallletters %>% 
  ggplot(aes(x=Years, y=Letter_Mean, color = Letters)) + 
  geom_line() + 
  geom_point()

reprex package(v2.0.1)于2023年2月12日创建

相关问题