将带有描述性注解的框添加到ggplot2中的y轴 [英] Add boxes with descriptive annotations to y-axis in ggplot2
本文介绍了将带有描述性注解的框添加到ggplot2中的y轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
推荐答案
实现此目的的一种方法是使用patchwork
。您可以将y轴的注释设置为第二个gggraph,并使用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))
创建于2020-05-28reprex package(v0.3.0)
这篇关于将带有描述性注解的框添加到ggplot2中的y轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文