Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: placing ggplot components separately #105

Open
pieterjanvc opened this issue Nov 17, 2023 · 2 comments
Open

Feature request: placing ggplot components separately #105

pieterjanvc opened this issue Nov 17, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@pieterjanvc
Copy link

Hello,

I just discovered plotgardener and think it's an amazing package which is going to save me a lot of time when generating multi-panel plots!

I'm also excited that it works with ggplot out of the box, but had and idea / request which would solve a lot of my frustrations: It would great if it was possible to separate ggplot components (plot, axes, legend, title/labs) and be able to place them as individual components using your framework. I often find myself cutting out a legend and moving it around because it's distorting the layout or it can be shared by multiple plots.

Thanks again for creating this cool package!
PJ

@nekramer
Copy link
Member

Hi PJ,

Thank you for checking out the package and for this awesome idea! We have always played with the idea of having more flexibility with ggplot2, so this would be a great addition. I would love to discuss further how you would envision the implementation of this feature. For example, would one function pull out the desired ggplot component and then this be input to plotGG? Or would it be preferable to have all functionality in one function call (maybe with additional parameters to specifically place the separate elements)? If you have any additional thoughts/contributing code, please let me and my fellow plotgardener developers know in this thread!

Best,
Nicole

@nekramer nekramer added the enhancement New feature or request label Nov 17, 2023
@pieterjanvc
Copy link
Author

Hello,

I took some time to try and explore this a bit more and it seems the cowplot package already took a stab at this, but unfortunately because they work on a relative scale, it's very hard to get it to fit on the absolute scale that makes plotgardener so great. I have created a reprex below to show you what I mean, but as you can see I had to perform a lot of trickery to get this working, and it would be great if there was a way to make this more intuitive.

library(ggplot2)
library(cowplot)
library(tidyr)
library(plotgardener)

# AIM: Try and put a ggplot legend with dimensions 1x1.5 inch at
#  absolute position 1,1 on a plotgardener plot

#Original ggplot
myPlot <- ggplot(iris %>% pivot_longer(-Species), 
                 aes(x = Species, y = value, fill = name)) +
  geom_bar(position='dodge', stat='identity')

myPlot

# Modify legend in preparation for absolute plotting (trial and error values)
myPlot = myPlot +
  theme(
    legend.key.height = unit(0.33, "inch"),
    legend.key.width = unit(0.27, "inch"),
    legend.margin = margin(-0.2)
  )

#Extract legend with cowplot::get_legend
plotLegend <- get_legend(myPlot)

#Try and plot with plotgardener at position 1,1 with width of 1 and height of 1.5
pageCreate(width = 3, height = 3, default.units = "inches")
plotGG(plot = plotLegend,
       x = 1, y = 1,width = 1, height = 1.5) 
#> gg[gg1]

Created on 2023-11-18 with reprex v2.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants