绘制 ggplot geom_boxplot 和 geom_jitter,由一个因子分隔并仅显示方框中值

Plot a ggplot geom_boxplot with geom_jitter, separated by a factor and showing only box median

我有一个 data.frame 有两个 factor 变量(下面 df 中的 typeage)和一个数字变量(value 在下面 df 中):

set.seed(1)
df <- data.frame(type = c(rep("t1", 8), rep("t2", 8), rep("t3", 8), rep("t4", 8), rep("t5", 8), rep("t6", 8)),
                 age = rep(c(rep("y", 4),rep("o", 4)), 6),
                 value = rep(c(runif(4, 5, 10), runif(4, 7.5, 12.5)), 6),
                 stringsAsFactors = F)
df$type <- factor(df$type, levels = c("t1", "t2", "t3", "t4", "t5", "t6"), ordered = T)
df$age <- factor(df$age, levels = c("y", "o"), ordered = T)

我想使用 Rggplot2df$value 绘制为抖动点,按 df$type 分组和颜色编码,但在 [=24= 内] 以 df$age 分隔。另外,我想显示每组 df$typedf$age.

的中线

到目前为止,我只能得到没有中线的点:

library(ggplot2)
ggplot(df, aes(x = age, y = value, color = type)) + 
  geom_jitter(width = 0.3) +
  facet_wrap(~type,nrow = 1) + theme_minimal()

知道如何添加中线吗?

您的示例数据对于所有类型都是相同的,因此我对其进行了一些更改:

set.seed(1)
df <- data.frame(type = c(rep("t1", 8), rep("t2", 8), rep("t3", 8), rep("t4", 8), rep("t5", 8), rep("t6", 8)),
                 age = rep(c(rep("y", 4),rep("o", 4)), 6),
                 value = runif(48, 5, 10),
                 stringsAsFactors = F)
df$type <- factor(df$type, levels = c("t1", "t2", "t3", "t4", "t5", "t6"), ordered = T)
df$age <- factor(df$age, levels = c("y", "o"), ordered = T)

您可以为此使用 stat_summary

ggplot(df) + 
  geom_jitter(aes(x = age, 
                  y = value,
                  color = type,
                  group = age),
              width = 0.2) +
  
  stat_summary(aes(x = age,
                   y = value,
                   color = type,
                   group = interaction(age, type)), 
               fun = median,
               geom = "crossbar") +
  
  scale_color_brewer(palette = "Dark2") +
  
  facet_wrap(~type,nrow = 1) + 
  
  theme_bw()