将轴标题移向绘图并保留组合矩阵标签

我正在尝试将 y 轴标题移向刻度。但是,在不切断组合矩阵标签的情况下,我无法做到这一点。我已经尝试过 ggupsetggplot 函数。请参阅下面的 reprex。

感谢您的帮助!

library(dplyr)
library(ggupset)
library(ggplot2)

tidy_pathway_member <- 
  gene_pathway_membership %>%
  as_tibble(rownames = "Pathway") %>%
  tidyr::gather(Gene,Member,-Pathway) %>%
  filter(Member) %>%
  select(- Member)

g <-
  tidy_pathway_member %>%
  group_by(Gene) %>%
  summarize(Pathways = list(Pathway)) %>%
  ggplot(aes(x = Pathways)) +
  geom_bar() +
  scale_x_upset()

g

将轴标题移向绘图并保留组合矩阵标签

g +
  # Moves axis title towards ticks...but cuts off labels
  theme_combmatrix(combmatrix.label.make_space = FALSE)

将轴标题移向绘图并保留组合矩阵标签

g +
  # Also,moves axis title towards ticks...but cuts off labels
  theme(axis.title.y = element_text(margin = margin(r = -100)))

将轴标题移向绘图并保留组合矩阵标签

reprex package (v2.0.0) 于 2021 年 7 月 30 日创建

xty2004 回答:将轴标题移向绘图并保留组合矩阵标签

这可以通过一种棘手的方式完成。

解决办法是隐藏y轴标题,在目标位置添加带有annotate()的文字。

由于你没有提供你的数据,我会在一个例子中展示它。

原图:

ggplot(data = diamonds,mapping = aes(x = clarity)) + geom_bar(aes(fill = cut))

enter image description here

使用 annotate 代替 y 轴标题的版本:

ggplot(data = diamonds,mapping = aes(x = clarity)) + geom_bar(aes(fill = cut)) +
  theme(axis.title.y=element_blank()) + annotate(geom = "text",x = -0.2,y = 6500,label = "count",angle = 90,size=4) + 
  coord_cartesian(xlim = c(1,8),clip = "off")

enter image description here

您只需要在x内设置适当的yxlim坐标以及coord_cartesian

本文链接:https://www.f2er.com/8729.html

大家都在问