Skip to content

Feature request: enable making copies of trained position scales #3441

Closed
@clauswilke

Description

@clauswilke

To create plots with marginals, it would be helpful if there was a straightforward method to reliably create copies of trained position scales. I'm wondering whether this is possible or can be made possible after @paleolimbot's refactoring of the position scales.

For example, consider this code, which uses the function cowplot::axis_canvas():

library(tidyverse)
library(cowplot)

p1 <- ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
  geom_point(alpha = 0.5)

p1

p2 <- axis_canvas(p1, "x", iris, aes(x = Sepal.Length, fill = Species)) +
  geom_density()

p2 + theme_grey()

p1 %>% insert_xaxis_grob(p2) %>% ggdraw()

I'd want to be able to generate the plot p2 by writing something like the following:

p2 <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
  geom_density() +
  scale_x_copy(p1)

or maybe

p2 <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
  geom_density() +
  scale_x_copy(ggplot_build(p1))

The function scale_x_copy() would take all information (type, limits, expansion, labels) about the x position scale from the built object of p1 and then faithfully reproduce it. Importantly, the scale should not change range or other properties based on the data provided in the second plot.

I'd be happy to implement such a scale myself if somebody can give me some hints about how to best go about it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions