将带有描述性注释的框添加到ggplot2中的y轴上

问题描述 投票:1回答:1

I“ M试图在我的Y轴上添加另一个标签或描述。我附上一张图片以作为我要完成的工作的参考。找不到任何描述如何在轴上添加其他元素的内容。它我要合并到ggplot中的Y轴旁边的“好”和“坏”框。谢谢!

enter image description here

ggplot2 axis annotate yaxis
1个回答
0
投票

实现此目的的一种方法是使用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创建

© www.soinside.com 2019 - 2024. All rights reserved.