r - 在 position_dodge() 之后的 ggplot2 中注释?

标签 r ggplot2 annotate

我正在尝试在 R 中生成一个双因子实验图。它包含三个级别,每个级别都有两个子级别。我想对这些组中的每一个进行注释,但我找不到合理的方法。

这是一个为顶级生成注释的示例:

ggplot(mtcars, aes(x = as.factor(cyl), y = mpg)) +
  geom_point(aes(colour = as.factor(am)), position = position_dodge(0.5)) +
  annotate("text", x = 1:3, y = c(37, 25, 22),
           label = c("a", "b", "c"))

我真正想做的是注释“我”的每个级别。

这是我天真的尝试:

ggplot(mtcars, aes(x = as.factor(cyl), y = mpg)) +
  geom_point(aes(colour = as.factor(am)), position = position_dodge(0.5)) +
  annotate("text", x = 1:6, y = c(27, 37, 25, 25, 22, 17),
           label = c("a", "b", "c", "d", "e", "f"), position = position_dodge(0.5))

> Error: Unequal parameter lengths: x (6), y (6), label (6), position (3)

我试过改变 x、y 和标签的数量。我怀疑答案在于为注释分配"is"级别,但我不知道该怎么做。谁有可行的解决方案?

最佳答案

也许试试 geom_text

library(ggplot2)

ggplot(mtcars, aes(x = cyl, y = mpg)) +
  geom_point(aes(colour = as.factor(am)), position = position_dodge(0.5)) +
  geom_text(data = data.frame(x = c(3.8, 4.2, 5.8, 6.2, 7.8, 8.2), 
                              y = c(27, 37, 25, 25, 22, 17)), 
  aes(x, y, label = c("a", "b", "c", "d", "e", "f")))

enter image description here

关于r - 在 position_dodge() 之后的 ggplot2 中注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56524845/

相关文章:

r - 使用 ggplot2 进行荟萃分析的子组图

r - 如何快速绘制小标题的一列与所有其他列的图表?

php - 如何使用 PHP 用 HTML 注释字符串(即如何通过偏移量维护有效的 HTML 将 HTML 标记插入字符串)?

python - Django 查询明确列出字段值的计数

r - 将蒙特卡罗 p 值排列成不同样本大小和方差估计量的矩阵

重新排列 ANOVA 的数据

r - 当我加载 ggfortify 时,自动绘图函数的行为有所不同

django - 如何使用 django annotate() 和 aggregate() 对查询集进行分组?

R 在列表列工作流程中使用 dplyr::select()

r - 参数比例在 r 中的 ggplot2 中不起作用