Trying to figure out how to get the y_position of the p value bars in the right place in an automated way, by calculating the max of {{contvar}}. Hoping to use max* 1.1 or max *1.2 to put these in staggered locations. Clearly doing something wrong. I want to get out a constant for max.
I would also like to use {glue} to glue in titles and axis labels that automatically include the correct catvar and contvar. This is not working in my few futile attempts. Things like `x = glue("Measurement of {{contvar}}"), which gives me "Measurement of {contvar}" as an x label.
# note this does the 3 pairs of catvars with contvars.
# to do all (9) combinations, let's set that up
# with an anaysis_list dataframe of all combos
analysis_list <- crossing(catvars, contvars)
# now for all possible (9) combinations
# This does NOT work
map2(.x = syms(analysis_list$catvars),
.y = syms(analysis_list$contvars),
.f = plot,
data = penguins) # all combos of x, y
#> Error in `map2()`:
#> ℹ In index: 1.
#> Caused by error in `as.double()`:
#> ! cannot coerce type 'symbol' to vector of type 'double'
#> Backtrace:
#> ▆
#> 1. ├─purrr::map2(...)
#> 2. │ └─purrr:::map2_("list", .x, .y, .f, ..., .progress = .progress)
#> 3. │ ├─purrr:::with_indexed_errors(...)
#> 4. │ │ └─base::withCallingHandlers(...)
#> 5. │ ├─purrr:::call_with_cleanup(...)
#> 6. │ ├─base (local) .f(.x[[i]], .y[[i]], ...)
#> 7. │ └─graphics::plot.default(.x[[i]], .y[[i]], ...)
#> 8. │ └─grDevices::xy.coords(x, y, xlabel, ylabel, log)
#> 9. └─base::.handleSimpleError(...)
#> 10. └─purrr (local) h(simpleError(msg, call))
#> 11. └─cli::cli_abort(...)
#> 12. └─rlang::abort(...)
I am a little bit surprised that this works, as pull(max) results in a long vector (the length of the data frame).
I had assumed that you would need a vector of length 1, but y_position = 1.2 * mv seems just fine with it.
To help me understand this better, why is this OK, and there is not an error/warning about y_position being a long vector when it only needs to be length 1?
But when I try to do all possible combinations with crossing, somehow it goes haywire. It looks quite similar, but there must be something subtle going wrong. Any help appreciated.
library(tidyverse)
library(palmerpenguins)
library(ggsignif)
library(glue)
plot_cats <- function(data, catvar, contvar){
mv <- transmute(data,
max := max({{contvar}},na.rm=TRUE)) |>
pull(max)
data |>
ggplot() +
aes(x = {{contvar}}, y = factor({{catvar}}),
fill=factor({{catvar}})) +
geom_boxplot() +
geom_jitter(width = 0.6) +
geom_signif(comparisons = list(c(1,2))) +
geom_signif(comparisons = list(c(1,3)),
y_position = 1.1 * mv) +
geom_signif(comparisons = list(c(2,3)),
y_position = 1.2 * mv) +
theme_bw() +
theme(legend.position = "none",
panel.grid.major.x = element_blank()) +
labs(y = "",
x = glue("Measurement of {ensym(contvar)}"),
title = glue("Comparison of {ensym(contvar)} \nAcross {ensym(catvar)} categories" ))
}
catvars <- c('island', 'species', 'sex')
contvars <- c('bill_length_mm', 'body_mass_g', 'flipper_length_mm')
analysis_list <- crossing(catvars, contvars)
# now for all possible (9) combinations
map2(.x = syms(analysis_list$catvars),
.y = syms(analysis_list$contvars),
.f = plot,
data = penguins) # all combos of x, y
#> Error in `map2()`:
#> ℹ In index: 1.
#> Caused by error in `as.double()`:
#> ! cannot coerce type 'symbol' to vector of type 'double'
#> Backtrace:
#> ▆
#> 1. ├─purrr::map2(...)
#> 2. │ └─purrr:::map2_("list", .x, .y, .f, ..., .progress = .progress)
#> 3. │ ├─purrr:::with_indexed_errors(...)
#> 4. │ │ └─base::withCallingHandlers(...)
#> 5. │ ├─purrr:::call_with_cleanup(...)
#> 6. │ ├─base (local) .f(.x[[i]], .y[[i]], ...)
#> 7. │ └─graphics::plot.default(.x[[i]], .y[[i]], ...)
#> 8. │ └─grDevices::xy.coords(x, y, xlabel, ylabel, log)
#> 9. └─base::.handleSimpleError(...)
#> 10. └─purrr (local) h(simpleError(msg, call))
#> 11. └─cli::cli_abort(...)
#> 12. └─rlang::abort(...)