ggplot 有很多类别

时间:2021-06-01 16:24:44

标签: r ggplot2

[![在此处输入图片描述][1]][1]我有一个数据框:

df <- data.frame(z = c(2,1,2,3,2,3,2,1,1,1,3,4,1,1,2,3,4,3),
                 x = c("a","a","a","a","a","a","b","b","b","b","b","b","c","c","c","c","c","c"),
                 cat = c("A","A", "B","B","C", "C", "A","A", "B","B","C","C","A","A", "B","B","C","C"))

我想在 ggplot 中绘制 x 轴分类值(3 个类别:a、b、c)。 在 y 轴上应该是“z”中值的平均值:

all x="a" and cat="A"
all x="b" and cat="B"
all x="c" and cat="C"

来自同一个“猫”的平均值(数据点)应该连接起来,并用对应于“猫”的 3 种不同颜色表示。

1 个答案:

答案 0 :(得分:3)

这应该可以满足您的需求:

library(tidyverse)
df %>% 
  group_by(cat,x) %>% 
  summarise(avg = mean(z)) %>% 
  ggplot(aes(x=x, y=avg, group=cat, color=cat)) + 
  geom_point() +
  geom_line()

编辑:如果您还需要 x 级别的总体均值,可以使用以下代码:

library(tidyverse)
df %>% 
  group_by(cat,x) %>% 
  summarise(avg = mean(z)) %>% 
  bind_rows(
      df %>%
      group_by(x) %>%
      summarise(avg=mean(z)) %>%
      mutate(cat="All")) %>%
  ungroup() %>% # This is needed to mutate the grouping variable "cat"
  mutate(cat = factor(cat, levels=c("A", "B", "C", "All"))) %>% # Otherwise you'd have "All" between "A" and "B"
  ggplot(aes(x=x, y=avg, group=cat, color=cat)) + 
  geom_point() +
  geom_line()