maia-sh
maia-sh

Reputation: 641

Move axis title towards plot and keep combmatrix labels

I am trying to move the y axis title towards the ticks. However, I'm having trouble doing this without cutting off the combmatrix labels. I've tried both with ggupset and ggplot functions. See reprex below.

Thank you for your help!

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

Created on 2021-07-30 by the reprex package (v2.0.0)

Upvotes: 2

Views: 397

Answers (2)

krassowski
krassowski

Reputation: 15379

Like this?

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

vjust=-30

BTW the same solution should work for ComplexUpset in principle.

Upvotes: 2

KacZdr
KacZdr

Reputation: 1599

This can be done in a tricky way.

The solution is to hide the y axis title and add text with annotate() to the target position.

Since you did not provide your data, I will show it on an example.

Original plot:

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

enter image description here

Version with annotate in place of the y-axis title:

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

You just need to set the appropriate x and y coordinates and xlim inside coord_cartesian.

Upvotes: 0

Related Questions