在 ggplot2 中将带有描述性注释的框添加到 y 轴
Add boxes with descriptive annotations to y-axis in ggplot2
I"M trying to add another label or description to my Y axis. I attached a picture for reference for what I'm trying to accomplish. I can't find anything that describes how to add additional elements to an axis. It the "Good" 和 Y 轴旁边的 "Bad" 框,我正试图将其合并到我的 ggplot 中。谢谢!
enter image description here
实现此目的的一种方法是使用 patchwork
。您可以将 y 轴的注释设置为第二个 ggplot,并使用 patchwork
将其粘贴到主图中。试试这个:
library(ggplot2)
library(patchwork)
library(dplyr)
p1 <- tibble(x = 1:10, y = 1:10) %>%
ggplot(aes(x, y)) +
geom_point() +
scale_y_reverse(breaks = seq(1, 10)) +
labs(y = NULL)
p2 <- tibble(ymin = c(0, 4), ymax = c(4, 10), fill = c("bad", "good")) %>%
ggplot() +
geom_rect(aes(xmin = 0, xmax = 1, ymin = ymin, ymax = ymax, fill = fill)) +
geom_text(aes(x = .5, y = (ymin + ymax) / 2, label = fill), angle = 90) +
scale_y_reverse(breaks = seq(1, 10), expand = expansion(mult = c(0, 0))) +
scale_x_continuous(breaks = c(0), expand = expansion(mult = c(0, 0))) +
guides(fill = FALSE) +
theme_void()
p2 + p1 + plot_layout(widths = c(1, 9))
由 reprex package (v0.3.0)
于 2020-05-28 创建
I"M trying to add another label or description to my Y axis. I attached a picture for reference for what I'm trying to accomplish. I can't find anything that describes how to add additional elements to an axis. It the "Good" 和 Y 轴旁边的 "Bad" 框,我正试图将其合并到我的 ggplot 中。谢谢!
enter image description here
实现此目的的一种方法是使用 patchwork
。您可以将 y 轴的注释设置为第二个 ggplot,并使用 patchwork
将其粘贴到主图中。试试这个:
library(ggplot2)
library(patchwork)
library(dplyr)
p1 <- tibble(x = 1:10, y = 1:10) %>%
ggplot(aes(x, y)) +
geom_point() +
scale_y_reverse(breaks = seq(1, 10)) +
labs(y = NULL)
p2 <- tibble(ymin = c(0, 4), ymax = c(4, 10), fill = c("bad", "good")) %>%
ggplot() +
geom_rect(aes(xmin = 0, xmax = 1, ymin = ymin, ymax = ymax, fill = fill)) +
geom_text(aes(x = .5, y = (ymin + ymax) / 2, label = fill), angle = 90) +
scale_y_reverse(breaks = seq(1, 10), expand = expansion(mult = c(0, 0))) +
scale_x_continuous(breaks = c(0), expand = expansion(mult = c(0, 0))) +
guides(fill = FALSE) +
theme_void()
p2 + p1 + plot_layout(widths = c(1, 9))
由 reprex package (v0.3.0)
于 2020-05-28 创建