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

Allow for easy insertion of ggplot plots in a table column via a formatter #155

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

rich-iannone
Copy link
Member

@rich-iannone rich-iannone commented Jan 31, 2019

The new fmt_ggplot() function allows for easier inclusion of ggplot2 plots (within a list column of the input table data) into a gt table. The common pattern toward obtaining these plots is through
mutation of a list column containing all the data required for plot:

<data> %>% 
  dplyr::group_by(<var>) %>%
  tidyr::nest(.key = plot) %>%
  dplyr::mutate(plot = purrr::map(plot, <ggplot code>))

While the PR contains functionality to detect and automatically format columns containing ggplot2 plots, we can also opt to use the fmt_ggplot() function to allow us to specify rows and set options for the plots' height and aspect_ratio.

Here are some examples of how this would work:

# Create a small table with a `plot` column
gtcars_plot_column <-
  gtcars %>%
  dplyr::group_by(mfr) %>%
  tidyr::nest(.key = plot) %>%
  dplyr::mutate(
    plot = map(plot, ~ggplot(., aes(hp, trq)) + geom_point())) %>%
  head(3)

# Automatic formatting of plots in `plot` using
# default options
tab_1 <- 
  gtcars_plot_column %>%
  gt()

# Using `fmt_ggplot()` to provide values
# for `columns`, `height` and `aspect_ratio`
tab_2 <- 
  gtcars_plot_column %>%
  gt() %>%
  fmt_ggplot(
    columns = vars(plot),
    height = 200,
    aspect_ratio = 2.5
  )

Here are images of tab_1 and tab_2:

tab_1

tab_2

These are minimal examples showing the basic usage of inserting plots, so, the resulting tables look less than ideal. With some extra care given to setting common axis ranges and using ggplot2::theme() to increase font sizes, line widths, etc., inserted plots would become more aesthetically pleasing.

Fixes #152

@rich-iannone rich-iannone changed the title Allow for easy insertion of ggplot plots in a table column via a formatter [WIP] Allow for easy insertion of ggplot plots in a table column via a formatter Feb 10, 2019
Copy link
Member

@jcheng5 jcheng5 left a comment

Choose a reason for hiding this comment

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

As discussed in person, I think all such features shouldn't be specific to ggplot but should work with all static R plots.

* master: (37 commits)
  Use webshot to generate images of HTML tables (#257)
  Rewritten vignette for adding summary rows (#273)
  Move roxygen documentation to Markdown (#253)
  Store empty-string value in `sep` vector (#274)
  Bugfixes and improvements to `summary_rows()` (#175)
  Modify _travis.yml (#263)
  Make several refactoring improvements to some `format_*()` functions (#244)
  Include option to hide all column labels (#237)
  Refactoring of formatter functions (#232)
  Improvement to the handling of a `pattern` in most `fmt*()` functions (#95)
  pkgdown cleanup (#226)
  Make argument names in `tab_options()` more consistent with terminology (#221)
  dplyr::data_frame deprecated (#195)
  Remove a unicode character from roxygen documentation (#220)
  Try again, this time with `travis encrypt --com`
  Trying again with github token (#223)
  Use jcheng5 GitHub PAT instead of rich-iannone (#222)
  Update README (#219)
  Fix for `fmt_date()` that allows `Date` columns to work (#203)
  Remove check on macOS
  ...
@rich-iannone rich-iannone mentioned this pull request Jul 2, 2019
@GregSutcliffe
Copy link

@rich-iannone any update on this? embedded plots came up again for me today, and I'm getting by with a rebased version of this branch... can I help at all?

@ciallen
Copy link

ciallen commented Jul 22, 2020

Hey! Any updates to this? I need to embed box plots in a table and would love to use this function. Thanks!

@ymer
Copy link

ymer commented May 23, 2021

It would be nice if this could be pushed. I think 90%+ of the use would be for ggplot, and it works well for that.

@rich-iannone rich-iannone changed the title [WIP] Allow for easy insertion of ggplot plots in a table column via a formatter Allow for easy insertion of ggplot plots in a table column via a formatter Nov 17, 2022
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.

adding ggplot graphs in every row of a table
5 participants