library(palmerpenguins)
library(janitor)
#> Warning: replacing previous import 'vctrs::data_frame' by 'tibble::data_frame'
#> when loading 'dplyr'
#>
#> Attaching package: 'janitor'
#> The following objects are masked from 'package:stats':
#>
#> chisq.test, fisher.test
library(glue)
# this is a simple example (minimal reprex) for a larger problem
data <- palmerpenguins::penguins
species_vec <- data %>% distinct(species) %>% pull()
#> Error in distinct(., species): could not find function "distinct"
data %>%
filter(species == "Adelie") %>%
tabyl(island) %>%
adorn_totals("row") %>%
arrange(desc(n)) ->
table1
#> Warning in data.matrix(data): NAs introduced by coercion
#> Warning in data.matrix(data): NAs introduced by coercion
#> Warning in data.matrix(data): NAs introduced by coercion
#> Error in filter(., species == "Adelie"): object 'species' not found
table1[1,1] <- "All Islands"
#> Error in table1[1, 1] <- "All Islands": object 'table1' not found
table2 <- table1 %>%
mutate(new_col = paste0(n, " ", island, "\n")) %>%
select(new_col)
#> Error in eval(lhs, parent, parent): object 'table1' not found
# store max character width for later use in box sizing
islands_char_width <- max(nchar(table2$new_col))
#> Error in nchar(table2$new_col): object 'table2' not found
island_label <- glue_collapse(table2$new_col)
#> Error in glue_collapse(table2$new_col): object 'table2' not found
island_label
#> Error in eval(expr, envir, enclos): object 'island_label' not found
# I would like to make an island_label
# (essentially a mini-table with line breaks) as a single string
# for each species of penguin
# and put the text on a ggplot with geom_text
# with a surrounding box (geom_rect) sized to the text
# but first I need to make the island_labels for each species.
# I would like to do this iteratively (over species_vec) with purrr and end up with a 1 column dataframe
# I can wrap this into a function, but I am struggling to make this work with purrr and map_dfr
#
# Or am I going about this in a wrongheaded way and is there a better way to plot mini tables in boxes
# requirement: in which I know exactly where the boxes are located (for drawing arrows between them later)?
Thank you!
I can just unlist() this result to get the vector of three strings that I need.
I was having the hardest time figuring out the purrr syntax.
I was trying to make it work with map_dfr(), but getting an error Error: Argument 1 must have names.
which I was struggling to understand.
Just for my understanding, is there a way to make this work with map_df() or map_dfr()?