set.seed(1014) options(digits = 3) knitr::opts_chunk$set( comment = "#>" ) # Multiple plot function from Winston Chang's R cookbook # Source: http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_%28ggplot2%29/ # # ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects) # - cols: Number of columns in layout # - layout: A matrix specifying the layout. If present, 'cols' is ignored. # # If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE), # then plot 1 will go in the upper left, 2 will go in the upper right, and # 3 will go all the way across the bottom. # multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) { library(grid) # Make a list from the ... arguments and plotlist plots <- c(list(...), plotlist) numPlots = length(plots) # If layout is NULL, then use 'cols' to determine layout if (is.null(layout)) { # Make the panel # ncol: Number of columns of plots # nrow: Number of rows needed, calculated from # of cols layout <- matrix(seq(1, cols * ceiling(numPlots/cols)), ncol = cols, nrow = ceiling(numPlots/cols)) } if (numPlots==1) { print(plots[[1]]) } else { # Set up the page grid.newpage() pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout)))) # Make each plot, in the correct location for (i in 1:numPlots) { # Get the i,j matrix positions of the regions that contain this subplot matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE)) print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row, layout.pos.col = matchidx$col)) } } }
Inspiriert bei visualising diurnal wind climatologies.
Die multiplot
Funktion ist kopiert von Winston Chang's R cookbook.
Die Verwendung von cowplot::plot_grid()
oder einer anderen Alternative ist empfohlen. Allerdings wurde an dieser Stelle darauf verzichtet um nicht eine zusätzliche Abhängigkeit zu erzeugen.
library(magrittr) library(ggplot2) library(rOstluft) fn <- rOstluft.data::f("Zch_Stampfenbachstrasse_h1_2013_Jan.csv") df <- read_airmo_csv(fn) df <- rolf_to_openair(df) df <- dplyr::mutate(df, hour = lubridate::hour(.data$date)) density <- ggplot(df, aes(wd, hour)) + stat_density_2d(aes(fill = stat(density)), geom = "raster", contour = FALSE, show.legend = FALSE, n = 200, na.rm = TRUE) + scale_fill_viridis_c() + scale_y_continuous(breaks = seq(0, 24, 4)) + geom_density2d(color = "black", na.rm = TRUE) + coord_cartesian(ylim = c(-0.5, 23.5), expand = FALSE) box <- ggplot(df, aes(x = hour, y = ws, group = hour)) + geom_boxplot(outlier.alpha = 0.1, na.rm = TRUE) + coord_flip(xlim = c(-0.5, 23.5), expand = FALSE) + theme(axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank()) multiplot(density, box, cols = 2)
# mit 30 min Daten fn <- rOstluft.data::f("Zch_Stampfenbachstrasse_2010-2014.csv") df <- read_airmo_csv(fn) df <- rolf_to_openair(df) df <- dplyr::mutate(df, season = rOstluft.plot::cut_season(.data$date), hour = lubridate::hour(.data$date) + lubridate::minute(.data$date) / 60 ) density <- ggplot(df, aes(wd, hour)) + stat_density_2d(aes(fill = stat(density)), geom = "raster", contour = FALSE, show.legend = FALSE, n = 200, na.rm = TRUE) + scale_fill_viridis_c() + scale_y_continuous(breaks = seq(0, 20, 4)) + geom_density2d(color = "black", na.rm = TRUE) + coord_cartesian(ylim = c(-0.5, 24), expand = FALSE) box <- ggplot(df, aes(x = hour, y = ws, group = hour)) + geom_boxplot(outlier.alpha = 0.1, na.rm = TRUE) + coord_flip(xlim = c(-0.5, 24), expand = FALSE) + theme(axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank()) multiplot(density, box, cols = 2)
# facetting density <- density + facet_wrap(vars(season), ncol = 1) box <- box + facet_wrap(vars(season), ncol = 1) multiplot(density, box, cols = 2)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.