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

add get_* functions for plot components #111

Merged
merged 5 commits into from
Oct 29, 2018

Conversation

malcolmbarrett
Copy link
Contributor

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
Copy link

codecov-io commented Oct 13, 2018

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%> (ø) ⬆️
R/get_titles.R 100% <100%> (ø)
R/get_axes.R 100% <100%> (ø)
R/get_panel.R 47.36% <47.36%> (-52.64%) ⬇️
R/get_plot_component.R 80% <80%> (ø)
R/themes.R 98.9% <0%> (+8.02%) ⬆️
R/plot_grid.R 95.41% <0%> (+38.16%) ⬆️
... 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.

Copy link
Contributor

@clauswilke clauswilke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have commented on a few specific lines. I wouldn't say I did a comprehensive review; I just flagged things I saw. Also, please add some regression tests, in particular for component names, so we have some warning in the future should any components change name.

R/get_panel.R Show resolved Hide resolved
R/get_panel.R Outdated Show resolved Hide resolved
R/get_plot_component.R Show resolved Hide resolved
R/get_plot_component.R Outdated Show resolved Hide resolved
@malcolmbarrett
Copy link
Contributor Author

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

@clauswilke
Copy link
Contributor

Yes, please go ahead.

@clauswilke
Copy link
Contributor

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
Copy link
Contributor Author

malcolmbarrett commented Oct 21, 2018

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...

Copy link
Contributor

@clauswilke clauswilke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for all your work. I made a few more comments. You can ignore the Travis issue, it's a separate problem that I'll have to look into.

NEWS Outdated Show resolved Hide resolved
R/get_axes.R Outdated Show resolved Hide resolved
R/get_axes.R Outdated Show resolved Hide resolved
R/get_panel.R Show resolved Hide resolved
R/get_panel.R Outdated Show resolved Hide resolved
tests/testthat/test_get_axes.R Outdated Show resolved Hide resolved
tests/testthat/test_get_axes.R Outdated Show resolved Hide resolved
tests/testthat/test_get_panel.R Outdated Show resolved Hide resolved
tests/testthat/test_get_titles.R Outdated Show resolved Hide resolved
tests/testthat/test_plot_components.R Outdated Show resolved Hide resolved
@malcolmbarrett
Copy link
Contributor Author

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 clauswilke merged commit 29e0631 into wilkelab:master Oct 29, 2018
@clauswilke
Copy link
Contributor

Thanks!

@malcolmbarrett malcolmbarrett deleted the plot_components branch October 29, 2018 15:42
@malcolmbarrett
Copy link
Contributor Author

Thanks, @clauswilke!

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

Successfully merging this pull request may close these issues.

3 participants