# Small function to display plots only if it's interactive
p_ <- GGally::print_if_interactive
if (require(Hmisc)) {
data(tips)
p_(ggally_summarise_by(tips, mapping = aes(x = total_bill, y = day)))
p_(ggally_summarise_by(tips, mapping = aes(x = day, y = total_bill)))
# colour is kept only if equal to the discrete variable
p_(ggally_summarise_by(tips, mapping = aes(x = total_bill, y = day, color = day)))
p_(ggally_summarise_by(tips, mapping = aes(x = total_bill, y = day, color = sex)))
p_(ggally_summarise_by(tips, mapping = aes(x = day, y = total_bill, color = day)))
# custom text size
p_(ggally_summarise_by(tips, mapping = aes(x = total_bill, y = day), size = 6))
# change statistic to display
p_(ggally_summarise_by(tips, mapping = aes(x = total_bill, y = day), text_fn = weighted_mean_sd))
# custom stat function
weighted_sum <- function(x, weights = NULL) {
if (is.null(weights)) weights <- 1
paste0("Total : ", round(sum(x * weights, na.rm = TRUE), digits = 1))
}
p_(ggally_summarise_by(tips, mapping = aes(x = total_bill, y = day), text_fn = weighted_sum))
}
Run the code above in your browser using DataLab