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

ggplot in gt rows: handle "empty" space within cells #1606

Open
ScaonE opened this issue Mar 15, 2024 · 1 comment
Open

ggplot in gt rows: handle "empty" space within cells #1606

ScaonE opened this issue Mar 15, 2024 · 1 comment

Comments

@ScaonE
Copy link

ScaonE commented Mar 15, 2024

Dear all,

I have trouble trying to control ggplots aspect within a gt table.
I started from this recipe : https://rfortherestofus.com/2023/10/ggplots-in-gt-tables

Below the relevant lines of my code:

  • Function to create the ggplot
plot_stacked <- function(arg1, arg2) {
# some hidden code
  ggplot(
    data = dummy_gg,
    mapping = aes(
      y = Univers,
      x = PDM,
      fill = fct_reorder(Laboratoire, PDM),
      label = paste0(Laboratoire, " ", round(PDM), "%")
    )
  ) +
    geom_bar(stat = "identity", width = 0.4) +
    geom_text(position = position_stack(vjust = 0.5), size = 17) +
    theme_void() + 
    theme(
      legend.position = "none",
      plot.margin = unit(rep(0, 4), "mm")
    )
}
  • Creating the gt table
dummy %>%
  gt() %>%
  text_transform(
    locations = cells_body(columns = "plot"),
    fn = function(column) {
      map(
        column,
        \(x) {
          x <- strsplit(x, split = ";")[[1]]
          plot_stacked(x[[1]], x[[2]])
        }
      ) |>
        ggplot_image(height = px(75), aspect_ratio = 8)
    }
  ) %>%
# some hidden code
  tab_options(
    data_row.padding = px(0)
  )
  • And here is my current output

=> What I'd like to handle are the "empty zones" (aka reduce them to the minimum size) around the horizontal stacked barplots (which I tried to highlight in red on the screenshot).
Can this be handled on the gt side?
Any tips ?

Best regards

@rich-iannone
Copy link
Member

rich-iannone commented May 23, 2024

If the padding is 0 then I imagine the plots have some top/bottom whitespace. Could you look at your table in a browser's developer tools and see whether this is the case?

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

No branches or pull requests

2 participants