const-ae/ggupset

Remove duplicate labels when using facet_grid()

EricSDavis opened this issue · 3 comments

This is a really useful package!

I came across a use case where I need to facet by a grouping factor while sharing a common y-axis. Unfortunately, the set labels from axis_combmatrix() do not behave the same way as the y-axis labels and duplicate for each facet. Am I missing an option to turn off these duplicated labels?

Here is a reproducible example:

## Load helper packages
library(ggplot2)
library(tidyverse, warn.conflicts = FALSE)
library(ggupset)

## Generate reproducible example
set.seed(123)
df <- data.frame(set = c(rep("set1", 100),
                         rep("set2", 100),
                         rep("set1_set2", 100),
                         rep("set1_set3", 100),
                         rep("set2_set3", 100),
                         rep("set3", 100)),
                 value = c(rnorm(100, 2),
                           rnorm(100, -2),
                           rnorm(100, 0.5),
                           rnorm(100, 0.4),
                           rnorm(100, 0.3),
                           rnorm(100, 0.2)),
                 group = c(rep("group1", 200),
                           rep("group2", 400)))

## Plot with facets
ggplot(df, aes(x = set, y = value)) +
  facet_grid(~group, scales = "free", space = "free") +
  geom_boxplot(outlier.colour = NA) +
  axis_combmatrix(sep = "_", levels = c("set1", "set2", "set3"))

image

Hi Eric,

thank you for the kind words :)

I am not quite sure, I understand what you are trying to achieve.

If you specify that facets are on the rows, the x-axis is combined:

ggplot(df, aes(x = set, y = value)) +
  facet_grid(rows = vars(group), scales = "free", space = "free") +
  geom_boxplot(outlier.colour = NA) +
  axis_combmatrix(sep = "_", levels = c("set1", "set2", "set3"))

image

Or do you want to use facet_wrap()?

I edited my original plot in illustrator to remove the duplicate labels:

image

All sets are labeled on the left-hand side and there are no collisions.

Ah, okay. I see...

That of course looks much nicer, however I am not sure how to do this, because when I internally draw the labels, there is no way to tell if this is a facet on the very left that should draw the labels or further to the right and suppress them :/

I am afraid the best way to get rid of those labels will involve Illustrator...