PhanstielLab / plotgardener

https://phanstiellab.github.io/plotgardener/
Other
299 stars 28 forks source link

Feature request: placing ggplot components separately #105

Open pieterjanvc opened 10 months ago

pieterjanvc commented 10 months ago

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 commented 10 months ago

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

pieterjanvc commented 10 months ago

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