【问题标题】:Labeling individual boxes in a ggplot boxplot在 ggplot boxplot 中标记单个框
【发布时间】:2018-06-10 07:18:28
【问题描述】:

我在 powerpoint 中为该图表添加了重要性字母。我想在 R 中的每个框上方添加重要字母。我可以修改我的 ggplot 代码以在每个框上方包含字母吗?

代码:

p1 <- ggplot(beta_data, 
             aes(x=reorder(interaction, distwu,FUN = median),
                            y=distwu, fill=interaction)) +
  geom_boxplot()+ 
  theme(legend.position="none", axis.text.x = element_text(angle = 45, hjust = 1))  +
  labs(x="Treatment interaction", y = "Distance to centroid") + 
  ylim(0,1.0)

【问题讨论】:

  • 您需要创建一个包含以下列的附加数据框: 1. x 轴上列出的项目(称为 x 列); 2. y 值(将是前四分位数,加上一个将其推到盒子边缘上方的值 - 称为 y 列); 3. 您希望为轴上的每个点显示的文本(称为该列标签)。然后您需要在您的ggplot 代码中添加以下层:geom_text(data = newdf, aes(x = x, y = y, label = label)

标签: r ggplot2 boxplot labeling


【解决方案1】:

stat_summary 的自动放置让生活变得简单。你没有分享任何数据,所以这里有一个例子:

ggplot(mtcars, aes(factor(cyl), mpg)) +
  geom_boxplot() +
  stat_summary(geom = 'text', label = letters[1:3], fun.y = max, vjust = -1)

【讨论】:

    猜你喜欢
    • 2019-10-19
    • 2022-08-15
    • 2018-12-08
    • 2018-08-31
    • 2019-02-06
    • 1970-01-01
    • 1970-01-01
    • 2018-03-28
    • 2018-05-28
    相关资源
    最近更新 更多