ggpattern icon indicating copy to clipboard operation
ggpattern copied to clipboard

New "stackdodge" position

Open trevorld opened this issue 3 years ago • 1 comments

  • A new {ggplot2} "position" that does both a "dodge" and "stack" might be useful:

    • https://github.com/coolbutuseless/ggpattern/issues/67
  • Technically this seems like this should be implemented as a new {ggplot2} "position" class:

    • https://ggplot2-book.org/extensions.html#new-positions
  • Not necessarily useful just for {ggpattern} (i.e. people seem to want to do this with unpatterned base geom_bar()):

    • https://stackoverflow.com/questions/46597278/how-to-plot-a-stacked-and-grouped-bar-chart-in-ggplot/46597859#46597859
  • Would {ggpattern} be the right level to implement this? Five years ago {ggplot2} maintainers said they had no (near-term) plans on implementing this particular "position" themselves: https://github.com/tidyverse/ggplot2/issues/2267

    • But what if they received a PR that implemented this?
    • {ggplot2} did add a "jitterdodge" position so perhaps "stackdodge" would be the appropriate name...

trevorld avatar Jun 24 '22 22:06 trevorld

Also note one can achieve a similar affect via facet_grid():

library(ggplot2)
library(ggpattern)

df <- transform(mpg, cyl = as.factor(cyl))

ggplot(df, aes(x = cyl)) +
    facet_grid(. ~ class) +
    geom_bar(aes(fill = cyl)) +
    geom_bar_pattern(aes(pattern = drv),
                     fill = "transparent", col = "black",
                     pattern_spacing = 0.2, pattern_density = 0.4,
                     pattern_key_scale_factor = 0.2) +
    scale_pattern_manual(values = c("none", "stripe", "circle"))

ggp_facet

trevorld avatar Jun 25 '22 02:06 trevorld