Open paco-ceam opened 4 years ago
Could use paste to make a new var that combines date, min, max and a line break
, then collapse(dataframe$new_var) to a single text string, then use geom_richtext. This ends up with one blank line at the end, but you could use str_extract and regex to remove the last
.
I think that this is a fairly common use case for small tables, and might be worth a function
Example (can likely be done much more elegantly)
iris %>% count(Species) %>% select(n, Species) -> table
table <- table %>% mutate(text_var = paste(as.character(n), as.character(Species), "<br>"))
text <- glue_collapse(table$text_var)
text <- str_extract(text, pattern = "^.+(?= <)")
I know it's been a while, but one option is to use gridExtra::tableGrob
and patchwork
(and, for now at least, not ggtext
):
library(ggplot2)
library(dplyr)
library(patchwork)
counts <- count(mtcars, cyl)
counts
# cyl n
# 1 4 11
# 2 6 7
# 3 8 14
tab <- gridExtra::tableGrob(counts, rows = rep("", nrow(counts)))
gg <- ggplot(mtcars, aes(mpg, disp)) + geom_point()
With that setup, we can do
gg + inset_element(tab, left = 0.7, bottom = 0.7, right = 1, top = 1)
gg + tab + plot_layout(widths = c(4, 1))
There are many theming options for the table, such as font, color (fg_params
), fill (bg_params
), etc. See its vignette, https://cran.r-project.org/web/packages/gridExtra/vignettes/tableGrob.html for a good start.
(This is applicable to https://github.com/wilkelab/ggtext/issues/39 as well.)
Hi everyone
I'm plotting a weekly temperature data series (values every 10 minutes) and would like to overlay a table with daily max/min temperature in my plot. Max/min temp are in a dedicated data.frame object. Is it possible to use geom_richtext in this case?
Thanks in advance