.TL R Cook Book .AU tropf .AB My personal recipes when using .URL "https://r-project.org/" "R" "." .AE .DA . .NH 1 Intro .PP I use R mainly for plotting and some data post-processing. This document collects some tricks I encountered and use. .PP For plotting I use .URL "https://github.com/tidyverse/ggplot2/" "ggplot2" "," which ist assumed to exist in these examples. A variable .CW "p" is typically a ggplot2 plot object. . .NH 1 Hiding Legend, but Preserving its Space .PP To hide a legend, but still take up its space (e.g. to build plots over multiple instances) add the following to the legend. Adjust the aesthetic used. .CB p <- p + guides(fill=guide_legend(override.aes=list(fill=NA))) + theme(legend.text=element_text(color="#00000000"), legend.title=element_text(color="#00000000")) .CE . .NH 1 aggregate() with more than one output value .PP To aggregate with multiple outputs, use the following snippet. Note that this only works if all outputs have the same type. .CB aggrs <- aggregate(to_be_summarized ~ category, df, function(v){c(mean=mean(v), sd=sd(v), min=min(v), max=max(v))}) extra_df <- data.frame(aggrs$to_be_summarized) aggrs$to_be_summarized <- NULL colnames(extra_df) <- paste("to_be_summarized", colnames(extra_df), sep=".") aggrs <- cbind(aggrs, extra_df) rm(extra_df) .CE