Closed cxinya closed 2 years ago
Howdy! Thanks for the feature request - do you have a minimal reprex example table that I can reference?
Yes! Thanks for taking a look. I'd love to make the pills just a little taller for rows like this.
library(dplyr)
library(gt)
library(gtExtras)
tibble(
x = "check<br>out<br>all<br>this<br>fab<br>data",
y = c(0, .5, 1)
) %>%
group_by(x) %>%
summarize(y = list(y)) %>%
gt() %>%
gt_plt_winloss(y, max_wins = 3) %>%
fmt_markdown(x)
Thanks for the reprex!
I've optimized all of the opinionated inline plots as essentially sparkline extensions, meaning they are all specifically written to take up as little vertical/horizontal space as possible. I try to be careful to not veer away from that, as otherwise I'm tasked with implementing one-off plots or failing to optimize, rather than the core focus of inline, minimalistic plots. Changing the overall scaling of winloss plots would be rather complex and could vary by HOW much vertical space do we need?
With the example you have - the overall scaling would be a problem - the plot would need to be both much wider and taller. If you want to optimize for a much different scale I'd recommend implementing it with ggplot2
itself.
A brief example below:
library(ggplot2)
library(dplyr)
library(gt)
df_ex <- tibble(
x = "check<br>out<br>all<br>this<br>fab<br>data",
y = c(0, .5, 1)
) %>%
group_by(x) %>%
summarize(y = list(y))
my_ggplot <- function(vals) {
df_gg <- data.frame(
x = 1:length(vals),
y = vals,
color = dplyr::case_when(
vals == 0.5 ~ "grey",
vals == 1 ~ "blue",
vals == 0 ~ "red")
)
print(df_gg$color)
ggplot(df_gg, aes(x = x)) +
geom_point(aes(y = y, fill = I(color), color = I(color)),
shape = 15, size = 40) +
theme_void() +
scale_x_continuous(limits = c(0.5, length(vals) + 0.5)) +
scale_y_continuous(limits = c(-0.1, 1.1))
}
ex_plot <- my_ggplot(df_ex$y |> unlist())
df_ex |>
gt() |>
text_transform(
locations = cells_body(y),
fn = function(x){
ggplot_image(ex_plot, aspect_ratio = 0.75)
}
) |>
fmt_markdown(x)
If you have some additional feedback, please let me know - otherwise going to close this one out for now. Thanks!
Hi @jthomasmock, I've been following this thread ever since a reviewer commented on a table I produced asking if I could re-size or edit the winloss plot in various ways.
This thread has been great for learning more about how to add a customized ggplot element, but I am a little unsure how to extend this example and implement it in a way that will respond dynamically to the input data the way that the gtExttras::plt_winloss
function behaves. And I imagine others might have this similar problem so it felt worth asking.
To be more specific, in the above example it is my understanding that the plot to include is hard-coded ahead of time as ex_plot
, and then inserted using ggplot_image()
. Working as desired for a single row of an input dataframe to gt()
, but needing some repeated code for each new plot, otherwise the same image is used for all rows.
To implement this in a way that would accept a list column similar to gtExtras::gt_plt_winloss()
, would you need to first generate a list of the appropriate ggplot objects/images? Or am I missing something that is right in front of me?
As always, great stuff, and thank you for sharing.
Thank you so much for making one of my favorite packages that makes my job easier almost every day!
It'd be helpful to add an option to control the height of the pills/squares in the
gt_plt_winloss()
function. In my case, I have a table with multi-line rows and the default pills appear a little small in their cells. It'd be great to be able to control height in addition to width.