【问题标题】:How can I add vertical lines and horizontal text out of the plot axis limits in ggplot2/R?如何在 ggplot2/R 的绘图轴限制之外添加垂直线和水平文本?
【发布时间】:2018-01-28 15:43:04
【问题描述】:

我有一个主要在 ggplot2 中制作的条形图。我想在 x 轴下方添加两条垂直线和一些文本。

#Load data
d <- structure(list(author = structure(c(1L, 2L, 4L, 3L, 5L, 6L, 8L, 11L, 13L, 12L, 10L, 9L, 7L), .Label = c("Bahr et al", "Fuller et al", "Garbossa et al", "Gokhale et al", "Iuchi et al", "Lee et al", "Lee Y et all", "Merrel et al", "Newton et al", "Rossetti et al", "Usery et al", "Wychowski et al", "Zachenhofer et al"), class = "factor"), nAE = c(-22L, -34L, -158L, -90L, -70L, -41L, -48L, -32L, -73L, -23L, -25L, -13L, -46L), AE = c(3L, 1L, 7L, 1L, 3L, 10L, 3L, 6L, 3L, 5L, 4L, 6L, 5L), SAE = c(0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 2L, 0L, 0L)), .Names = c("author", "nAE", "AE", "SAE"), class = "data.frame", row.names = c(NA, -13L))

我的条形图代码:

library(dplyr)
library(tidyr)
library(ggplot2)

categories <- c("Adverse Effect", "No adverse effects", "Severe side effects")
cols <- c("#f6766d", "#01bfc4", "orange")


q <- d %>% 
gather(key, value, -author) %>% 
ggplot(aes(author, value, fill = key)) +
geom_col(alpha=0.9) + 
scale_x_discrete(name="Author") +
scale_y_continuous(name="Number of observations", limits=c(-160,15), 
seq(-160, 15, by=10)) +
theme_grey() +
theme(legend.position = "top") +
scale_fill_manual(labels = categories, values = cols) + 
labs(fill = "")

我在下面附上了一张我希望我的条形图看起来如何的图片。如您所见,我添加了两条垂直线(随机位置)和三个文本(在 Photoshop 中)。

提前致谢, C.

【问题讨论】:

标签: r plot ggplot2 graph


【解决方案1】:

垂直线完全没有问题。只需使用:

  +
  geom_vline(xintercept= 3.5, colour = "red") + 
  geom_vline(xintercept= 10.5, colour = "red")

值 3.5 和 10.5 表示线截取第三和第四之间以及第十和第十一作者之间的 x 轴。

但是,在情节之外添加文本是完全不同的野兽。我能想到的“最干净”的方式是在情节中添加文本:

y <- min(d$nAE) + 10
textaes <- data.frame(y = c(y, y, y),
                      x = c(2, 7, 12),
                      lab = c("Text1", "Text2", "Text3"))
q <- d %>% 
  gather(key, value, -author) %>% 
  ggplot(aes(x=author, y=value, fill = key)) +
  geom_col(alpha=0.9) + 
  scale_x_discrete(name="Author") +
  scale_y_continuous(name="Number of observations", limits=c(-160,15), 
                     seq(-160, 15, by=10), expand = c(0.15, 0.05)) +
  theme_grey() +
  theme(legend.position = "top",
        axis.text.x = element_text(angle = 90, hjust = 1)) +
  scale_fill_manual(labels = categories, values = cols) + 
  labs(fill = "") +
  geom_vline(xintercept= 3.5, colour = "red") + 
  geom_vline(xintercept= 10.5, colour = "red") +
  geom_text(mapping = aes(y = y, x = x, label = lab), 
            data = textaes, inherit.aes = FALSE)
q

编辑:刚刚找到了一种相对简单的方法来在情节here 之外添加文本。但我不认为这是一个很好的解决方案:

q <- d %>% 
  gather(key, value, -author) %>% 
  ggplot(aes(x=author, y=value, fill = key)) +
  geom_col(alpha=0.9) + 
  scale_x_discrete(name="Author") +
  scale_y_continuous(name="Number of observations", limits=c(-160,15), 
                     seq(-160, 15, by=10), expand = c(0.15, 0.05)) +
  theme_grey() +
  theme(legend.position = "top",
        axis.text.x = element_text(angle = 90, hjust = 1)) +
  scale_fill_manual(labels = categories, values = cols) + 
  labs(fill = "") +
  geom_vline(xintercept= 3.5, colour = "red") + 
  geom_vline(xintercept= 10.5, colour = "red")
q

library(grid)
q
grid.text("Text1", x = unit(0.15, "npc"), y = unit(0.1, "npc"), gp=gpar(col="red"))
grid.text("Text2", x = unit(0.5, "npc"), y = unit(0.1, "npc"), gp=gpar(col="red"))
grid.text("Text3", x = unit(0.85, "npc"), y = unit(0.1, "npc"), gp=gpar(col="red"))

【讨论】:

  • 您也可以将文本颜色更改为红色吗?你知道,文本 1 + 文本 2 + 文本 3。谢谢 :)
  • 我试过了 - 但是当我在 geom_text 中应用 colour="red" 时,它会在顶部图例中显示“color Red”的位置添加一个“strata”。
  • 只需在 geom_text 中的 aes 外添加 , colour = "red"。例如。 geom_text(mapping = aes(y = y, x = x, label = lab), data = textaes, inherit.aes = FALSE, colour = "red").
  • 谢谢!最后一个问题。我希望 x 轴以与数据表等效的数字顺序显示,而不是按字母顺序显示。你也能解决吗?谢谢:)
  • 这是因为您的因子水平是按字母顺序排列的。您可以使用 d$author&lt;- factor(d$author, levels = as.character(d$author)) 简单地重新排序它们。
【解决方案2】:

gridExtra 包会有帮助吗?它应该提供足够接近的东西。您可以使用 arrangeGrobgrid.arrange 方法将 3 个图合并为 1 个。

https://cran.r-project.org/web/packages/gridExtra/index.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-21
    • 2020-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多