daattali / ggExtra

📊 Add marginal histograms to ggplot2, and more ggplot2 enhancements
http://daattali.com/shiny/ggExtra-ggMarginal-demo/
Other
380 stars 48 forks source link

support title alignment with the plot introduced in `ggplot2 3.3.0` #152

Open IndrajeetPatil opened 4 years ago

IndrajeetPatil commented 4 years ago

The latest (3.3.0) release of ggplot2 introduced a way to align the plot titles with the plot:

# setup
set.seed(123)
library(ggplot2)
library(ggExtra)

# basic plot
p <- 
  ggplot(mtcars, aes(wt, mpg)) + geom_point() +
  labs(title = "this title is aligned with the plot and not the axis") + 
  theme(plot.title.position = "plot")

# title aligned with the plot
plot(p)

But this alignment is removed by ggExtra:

# setup
set.seed(123)
library(ggplot2)
library(ggExtra)

# basic plot
p <- 
  ggplot(mtcars, aes(wt, mpg)) + geom_point() +
  labs(title = "this title is aligned with the plot and not the axis") + 
  theme(plot.title.position = "plot")

# alignment removed
ggMarginal(p)

crew102 commented 4 years ago

I'm not sure if this would be all that feasible to support. We pull the title/subtitle grob out of the built scatter plot (see getTitleGrobs()). If the built scatter plot doesn't include modifications to the title/subtitle that come from theme() (e.g., plot.title.position), there wouldn't be an easy way for us to include those modifications. I suppose we could check for modifications such as alignment by inspecting the theme of the built scatter plot and applying those changes to the title/subtitle grob in a piecemeal way (i.e., one by one), but that would be kinda a pain.