teunbrand / ggnomics

A small project to add ggplot2 extensions
https://teunbrand.github.io/ggnomics/
Other
80 stars 4 forks source link

Fixed x-axis labels per nested grid #41

Closed michelleklsmn closed 3 years ago

michelleklsmn commented 3 years ago

Hi,

I have been using facet_nested() quite a lot recently. Thank you for creating such a useful tool! I was wondering if it was possible to create a fixed x-axis per nested grid. Currently, the plot looks like this:

image

This is the code that I used (displaying only the relevant grid parameters):

ggplot(KEGG_df) +  
  facet_nested(~ Enrichment + ControlDesign, nest_line = TRUE,
               scale = "free", space = "free_x") +
  geom_point(mapping = aes(x = IncubationTime, y = KEGGdescr, 
                           color = -log10(FDR), size = EnrichmentScore)) +
  theme(strip.text = element_text(size = 8))

The x-axis of the grid 'Down' shows all labels. In contrast, the labels '4h' and '120h - 24h' are missing from the grid 'Up' since these conditions have no upregulated KEGG pathways. The plot layout that I would like to create is:

image

I have tried using the parameter scale = "fixed" instead of scale = "free". However, this caused that all x-axis labels are displayed in each grid:

image

Is there a way to fix the x-axis labels per nested grid? Meaning that the labels for both grids of 'Control: 0h' are identical and that the labels of both grids of 'Control: previous incubation time' are identical. If you require more information, please let me know. Thank you in advance!

Kind regards, Michelle

teunbrand commented 3 years ago

Well there is nothing to be done from the facet_nested() point of view, this is just how ggplot works in facet_grid() too (on which the facet_nested() is based).

However, I too shared your frustration, which is why I made the facetted_pos_scales() function that can tweak individual panel scales. Simplified example follows.

Let's make some data that should have 2 types of x-axis scales: A,B,C and D,E.

library(ggh4x)
#> Loading required package: ggplot2

df <- data.frame(
  x = c(LETTERS[1:5], LETTERS[1:5]),
  y = rnorm(10),
  outer = rep(c("Up", "Down"), each = 5),
  inner = rep(c("A", "A", "A", "B", "B"), 2)
)

Now, when we delete a relevant observation, we get something similar to the problem you mention: there is a C label missing from the 3rd panel x-axis.

# Delete observation C from A/Up panel
df <- df[-3,]

g <- ggplot(df, aes(x, y)) +
  geom_point() +
  facet_nested(~ outer + inner, scales = "free_x", space = "free")
print(g)

The way I'd deal with this, is to manually set the x scale for the 3rd panel, which you can do as follows:

g + facetted_pos_scales(x = list(
  NULL, NULL, # First two panels don't need adjusting, so they're NULL
  scale_x_discrete(limits = LETTERS[1:3])
))

Created on 2021-02-04 by the reprex package (v1.0.0)

And by setting the limits for the 3rd panel manually, we've restored the C label in the 3rd panel. Does this approach fix your problem?

michelleklsmn commented 3 years ago

Hi Teun,

This definitely helps me with my problem. Thank you for your help and quick reply!

Michelle