我有一个下面的R码

pdf <- data.frame(xx = as.factor(c(rep(0, 100), rep(1, 100))),
                  value = rnorm(200),
                  selected_site = c('y', rep('n', 98), 'y'),
                  name = as.factor(rep(1:5, each = 20)),
                  version = rep(c('A', 'B'), each = 100))

pdf %>%
  ggplot(aes(x = value, fill = xx)) +
  geom_histogram(alpha=0.6, position="identity") +
  facet_grid(selected_site ~ name, scale = 'free_y') + 
  scale_fill_manual(name = '', values = c('red', 'blue')) +
  facetted_pos_scales(
    y = list(selected_site == 'n' ~ scale_y_continuous(limits = c(0,5)),
             selected_site == 'y' ~ scale_y_continuous(limits = c(0,2)))) +
  xlab('') + ylab('THIS IS A TEST') +
  theme_bw() + 
  theme(legend.position = 'none',
        strip.background.y = element_blank(),
        strip.text.y = element_blank(),
        panel.spacing=unit(0.2, "lines"))

That produces this: enter image description here

我想为y轴标题创建一个背景,其中背景框高度与灰色阴影的绘图高度相匹配.

enter image description here

这有可能做到吗?也许和TextGrob一起?

推荐答案

我可能会通过使用拼图在主图的左侧添加一个细长的纯文本gggraph来实现此目的

library(ggh4x)
library(patchwork)

p2 <- pdf %>%
  ggplot(aes(x = value, fill = xx)) +
  geom_histogram(alpha=0.6, position="identity") +
  facet_grid(selected_site ~ name, scale = 'free_y') + 
  scale_fill_manual(name = '', values = c('red', 'blue')) +
  facetted_pos_scales(
    y = list(selected_site == 'n' ~ scale_y_continuous(limits = c(0,5)),
             selected_site == 'y' ~ scale_y_continuous(limits = c(0,2)))) +
  xlab('') + ylab(NULL) +
  theme_bw() + 
  theme(legend.position = 'none',
        strip.background.y = element_blank(),
        strip.text.y = element_blank(),
        panel.spacing=unit(0.2, "lines"))

p1 <- ggplot(data.frame(x = 1, y = 1, label = "THIS IS A TEST"), aes(x, y)) +
  geom_text(aes(label = label), angle = 90) +
  coord_cartesian(clip = "off") +
  theme_void() +
  theme(panel.background = element_rect(fill = "gray"),
        plot.margin = margin(5, 5, 5, 5))

p1 + p2 + plot_layout(widths = c(1, 30))

enter image description here

R相关问答推荐

R的法国工作日

使用列表列作为case_when LHS的输入

有没有方法将paste 0功能与列表结合起来?

提取rame中对应r中某个变量的n个最小正值和n个最大负值的条目

创建重复删除的唯一数据集组合列表

R的GG平行坐标图中的排序变量

如何在R中正确对齐放射状图中的文本

管道末端运行功能

为什么在ggplot2中添加geom_text这么慢?

如何通过Docker部署我的shiny 应用程序(多个文件)

如何在编辑列时更新可编辑数据表,并使用该表在Shiny中执行连续计算

将嵌套列表子集化为嵌套列表

如何优化向量的以下条件赋值?

par函数中的缩写,比如mgp,mar,mai是如何被破译的?

当我们有多个特殊字符时,使用gsub删除名称和代码'

使用R中的dist()迭代ID匹配的欧几里德距离

在数据帧列表上绘制GGPUP

删除在R中的write.table()函数期间创建的附加行

重写时间间隔模糊连接以减少内存消耗

如何在R中使用因子行求和?