ggplot2从图旁边的其他数据框中添加数据

时间:2018-08-09 02:23:08

标签: r ggplot2 boxplot

我希望能够为我的箱线图提供更多信息。这是ggplot2的工作示例:

library(ggplot2)

ToothGrowth$dose <- as.factor(ToothGrowth$dose)

# Basic box plot
p <- ggplot(ToothGrowth, aes(x=dose, y=len)) + 
  geom_boxplot()
# Rotate the box plot
p + coord_flip()

我想从一个单独的数据框中添加其他信息。例如:

extra <- data.frame(dose=factor(c(0.5,1,2)), label=c("Label1", "Label2", "Label3"), n=c("n=42","n=52","n=35"))

> extra
  dose  label    n
1  0.5 Label1 n=42
2    1 Label2 n=52
3    2 Label3 n=35

我想创建下图,其中每个剂量(因子)的信息都位于图的外部,并且与每个剂量水平一致(我在powerpoint中以此为例):

enter image description here

任何建议都将不胜感激!

关于, 卢克

编辑: 非常感谢您的回答!完善。我想请教扩展最初的问题。

在我使用填充剂将剂量分成两组时,该扩展名又如何呢?

ToothGrowth$dose <- as.factor(ToothGrowth$dose)
ToothGrowth$group <- head(rep(1:2, 100), dim(ToothGrowth)[1])
ToothGrowth$group <- factor(ToothGrowth$group)

 p <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=group)) + 
     geom_boxplot()
 # Rotate the box plot
 p + coord_flip()

extra <- data.frame(
  dose=factor(rep(c(0.5,1,2), each=2)), 
  group=factor(rep(c(1:2), 3)), 
  label=c("Label1A", "Label1B", "Label2A", "Label2B", "Label3A", "Label3B"), 
  n=c("n=12","n=30","n=20", "n=32","n=15","n=20")
)

是否可以将新数据框中的数据(额外的6行)与每个剂量/组组合对齐?

干杯, 卢克

1 个答案:

答案 0 :(得分:2)

我们可以将geom_text中的clip = "off"coord_flip一起使用:

ggplot(ToothGrowth, aes(x=dose, y=len)) +
    geom_boxplot() +
    geom_text(
        y = max(ToothGrowth$len) * 1.1,
        data = extra,
        aes(x = dose, label = sprintf("%s\n%s", label, n)),
        hjust = 0) +
    coord_flip(clip = "off") +
    theme(plot.margin = unit(c(1, 5, 0.5, 0.5), "lines"))

enter image description here

说明:我们使用geom_text将文本放置在绘图区域之外,并使用clip = "off"内的coord_flip禁用剪切。最后,我们增加了打印边距以容纳其他标签。您可以通过更改y中的系数来调整页边距中的垂直y = max(ToothGrowth$len) * 1.1位置(因此,由于坐标翻转而导致绘图中的水平位置)。


根据您的修改,有可能

extra <- data.frame(
  dose=factor(rep(c(0.5,1,2), each=2)),
  group=factor(rep(c(1:2), 3)),
  label=c("Label1A", "Label1B", "Label2A", "Label2B", "Label3A", "Label3B"),
  n=c("n=12","n=30","n=20", "n=32","n=15","n=20")
)

library(tidyverse)
ToothGrowth %>%
    mutate(
        dose = as.factor(dose),
        group = as.factor(rep(1:2, nrow(ToothGrowth) / 2))) %>%
    ggplot(aes(x = dose, y = len, fill = group)) +
    geom_boxplot(position = position_dodge(width = 1)) +
    geom_text(
        data = extra %>%
            mutate(
                dose = as.factor(dose),
                group = as.factor(group),
                ymax = max(ToothGrowth$len) * 1.1),
        aes(x = dose, y = ymax, label = sprintf("%s\n%s", label, n)),
        position = position_dodge(width = 1),
        size = 3,
        hjust = 0) +
    coord_flip(clip = "off", ylim = c(0, max(ToothGrowth$len))) +
    theme(
        plot.margin = unit(c(1, 5, 0.5, 0.5), "lines"),
        legend.position = "bottom")

enter image description here

一些评论:

  1. 我们通过在position_dodge(with = 1)geom_text中使用geom_boxplot来确保标签与闪避的条匹配。
  2. 似乎position_dodge不喜欢全局y(在aes之外)。因此,我们在extra中包括标签的y位置,并在aes中使用它。结果,我们需要显式限制y轴的范围。我们可以在coord_flip中使用ylim = c(0, max(ToothGrowth$len))来做到这一点。
相关问题