【问题标题】:Move axis title towards plot and keep combmatrix labels将轴标题移向绘图并保留组合矩阵标签
【发布时间】:2021-10-06 04:15:16
【问题描述】:

我正在尝试将 y 轴标题移向刻度。但是,如果不切断组合矩阵标签,我会遇到麻烦。我已经尝试过 ggupsetggplot 函数。请参阅下面的代表。

感谢您的帮助!

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 日创建

【问题讨论】:

  • 与您的数据或样本分享dput()

标签: r ggplot2 upsetr upsetplot


【解决方案1】:

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

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

由于您没有提供您的数据,我将在示例中显示它。

原图:

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

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")

您只需要在coord_cartesian 中设置适当的xy 坐标和xlim

【讨论】:

  • 感谢您的解决方法建议。如果有 ggupset 原生的解决方案,那就太好了。数据随包自带,所以ggupset:: gene_pathway_membership
  • 不幸的是,annotate 不能与 ggupset 一起工作,我相信,由于特殊的 x 轴。还有其他建议吗? “f(..., self = self) 中的错误:美学 'x' 的 scale_upset 错误。'x' 必须是列表类型。目前是:字符”
【解决方案2】:

像这样?

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() +
  # the exact vjust number needed may vary depending on the plotting area size
  theme(axis.title.y = element_text(vjust=-30))

g

顺便说一句,原则上相同的解决方案应该适用于 ComplexUpset。

【讨论】:

    猜你喜欢
    • 2021-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多