wilkelab / cowplot

cowplot: Streamlined Plot Theme and Plot Annotations for ggplot2
https://wilkelab.org/cowplot/
704 stars 84 forks source link

add get_* functions for plot components #111

Closed malcolmbarrett closed 5 years ago

malcolmbarrett commented 6 years ago

This PR expands cowplot's functionality for extracting plot components. Closes #110.

The main addition is get_plot_component(), which extracts grobs from a ggplot by matching a pattern to the grob names. I've added several wrappers for axes and titles and added a couple functions for getting component names (plot_component_names()) and quickly extracting the whole list of grobs (plot_components()). I've also rewritten get_legend() and get_panel() to use get_plot_components(). get_legend() works the same way, but get_panel() can now also specify a panel in a faceted plot.

I avoided the issue of pulling a list of grobs because both + and (by extension) ggdraw() have trouble with them. It would probably be possible to make ggdraw() support that, but it also doesn't really know how to put them back together. One option for pulling all facet panels is a function that puts them into a gtable, as I believe is done internally in ggplot2.

This needs a little cleaning up and better documentation before it's a proper PR, but I'd thought I'd better check with you before I put that work in.

Here's a demo of the new/rewritten functions:

library(tidyverse)
library(cowplot)

p <- ggplot(mtcars, aes(mpg, hp)) +
  geom_point()

p2 <- p +
  facet_wrap(~factor(gear), ncol = 2)

ggdraw(get_x_axis(p))

ggdraw(get_y_axis(p))

ggdraw(get_title(p + ggtitle("Here's my title", "And my subtitle")))

ggdraw(get_subtitle(p + ggtitle("Here's my title", "And my subtitle")))

ggdraw(get_legend(p + aes(col = am, shape = factor(gear))))


plot_component_names(p)
#>  [1] "background" "spacer"     "axis-l"     "spacer"     "axis-t"    
#>  [6] "panel"      "axis-b"     "spacer"     "axis-r"     "spacer"    
#> [11] "xlab-t"     "xlab-b"     "ylab-l"     "ylab-r"     "subtitle"  
#> [16] "title"      "caption"    "tag"
ggdraw(get_plot_component(p, "ylab-l"))

ggdraw(get_panel(p))


ggdraw(get_panel_component(get_panel(p), "geom_point"))


ggdraw(get_panel(p2, 3))

Created on 2018-10-13 by the reprex package (v0.2.0).

codecov-io commented 6 years ago

Codecov Report

Merging #111 into master will increase coverage by 14.51%. The diff coverage is 69.56%.

Impacted file tree graph

@@             Coverage Diff             @@
##           master     #111       +/-   ##
===========================================
+ Coverage   51.64%   66.16%   +14.51%     
===========================================
  Files          13       16        +3     
  Lines         697      733       +36     
===========================================
+ Hits          360      485      +125     
+ Misses        337      248       -89
Impacted Files Coverage Δ
R/get_legend.R 100% <100%> (ø) :arrow_up:
R/get_titles.R 100% <100%> (ø)
R/get_axes.R 100% <100%> (ø)
R/get_panel.R 47.36% <47.36%> (-52.64%) :arrow_down:
R/get_plot_component.R 80% <80%> (ø)
R/themes.R 98.9% <0%> (+8.02%) :arrow_up:
R/plot_grid.R 95.41% <0%> (+38.16%) :arrow_up:
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 374c3e9...fa94c73. Read the comment docs.

malcolmbarrett commented 6 years ago

Great, I'll make those changes next week. Overall, is it agreeable enough that I devote some time to fleshing out the documentation?

clauswilke commented 6 years ago

Yes, please go ahead.

clauswilke commented 6 years ago

Please also add a bullet point to NEWS, with your github user name and PR number, as explained in the tidyverse style guide: http://style.tidyverse.org/news.html#acknowledgement

malcolmbarrett commented 6 years ago

Ok: made those changes, expanded docs, and added some tests. Not sure what's up with the Travis build. A quick look doesn't look like it's related to changes, but not sure why it would suddenly fail on this branch...

malcolmbarrett commented 5 years ago

All added. I think the Travis thing was a one-off upstream change on a distant remote repo (some r-lib upstream dependency) that already got fixed there.

clauswilke commented 5 years ago

Thanks!

malcolmbarrett commented 5 years ago

Thanks, @clauswilke!