--- title: "Datenvisualisierung in R mit ggplot2" author: "Jens Müller" output: pdf_document: default html_document: default date: "2025-04-14" --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ``` ```{r} library(readxl) # for reading Excel files library(ggplot2) # for creating charts and plots library(RColorBrewer) # a popular color palette library(forcats) # for sorting factor variables library(sf) # for reading shape files library(dplyr) # for data manipulation library(tidyr) # for drop_na ``` # Datenvisualisierung ## Dates des Marteloskops Möllergrab laden ```{r} df <- read_excel('data/Marteloscope_Möllergrab_2021_DE.xlsx', sheet = '2021') df ``` ## Beschreibende Statistiken ```{r} summary(df) ``` ```{r} # we ignore the missing data for this notebook df_clean <- df |> drop_na(`h [m]`) df_clean ``` ```{r} summary(df_clean) ``` ## Visualisierungen ```{r} # consistent colors for tree species colors_tree_species <- brewer.pal(8, "Set2") ``` ### Histogramm ```{r} ggplot(data = df_clean, aes(x = `h [m]`)) + geom_histogram(binwidth=1, fill = 'steelblue', color = 'white') + labs(title = 'Histogramm der Baumhöhen', x = 'Höhe (m)', y = 'Anzahl der Bäume') + theme_minimal() ``` ### Gestapeltes Säulendiagramm (Stacked Bar Chart) ```{r} ggplot(data = df_clean, aes(x = `h [m]`, fill = factor(`Baumart`))) + geom_histogram(binwidth=1) + labs(title = 'Histogramm der Baumhöhen gestapelt nach Baumart', x = 'Höhe (m)', y = 'Anzahl der Bäume', fill = 'Baumart') + scale_fill_manual(values = colors_tree_species) + theme_minimal() ``` ```{r} ggplot(data = df_clean, aes(x = `h [m]`, fill = factor(`Baumart`))) + geom_histogram(position = 'dodge', bins = 40) + labs(title = 'Histogramm der Baumhöhen nach Baumart', x = 'Höhe (m)', y = 'Anzahl der Bäume', fill = 'Baumart') + scale_fill_manual(values = colors_tree_species) + theme_minimal() ``` ### Säulendiagramm ```{r} ggplot(data = df_clean, aes(x = `h [m]`, fill = factor(`Baumart`))) + geom_bar(position = 'dodge') + labs(title = 'Säulendiagramm der Baumhöhen mit Baumarten', x = 'Höhe (m)', y = 'Anzahl der Bäume', fill = 'Baumart') + scale_fill_manual(values = colors_tree_species) + theme_minimal() ``` ### Balkendiagramm ```{r} ggplot(data = df_clean, aes(x = `h [m]`, fill = factor(`Baumart`))) + geom_histogram(position = 'dodge', bins = 40) + coord_flip() + # for a horizontal bar chart labs(title = 'Balkendiagramm der Baumhöhen mit Baumarten', x = 'Höhe (m)', y = 'Anzahl der Bäume', fill = 'Baumart') + scale_fill_manual(values = colors_tree_species) + theme_minimal() ``` ### Streudiagramm (Scatter Plot) ```{r} ggplot(data = df_clean, aes(x = `d 1.3 [cm]`, y = `h [m]`, color = `Baumart`)) + geom_point(size = 2, alpha = 0.7) + labs(title = 'Brusthöhendurchmesser und Baumhöhe für unterschiedliche Baumarten\nim Marteloskop Möllergrab in 2021', x = 'Brusthöhendurchmesser (cm)', y = 'Höhe (m)') + scale_color_manual(values = colors_tree_species) + theme_minimal() ``` ### Liniendiagramm (Line Chart) ```{r} ggplot(data = df_clean |> arrange(`d 1.3 [cm]`), aes(x = `d 1.3 [cm]`, y = `G [m²]`)) + geom_point() + geom_line() + labs(title = 'Grundflächen in Abhängigkeit zum BHD im Marteloskop Möllergrab in 2021', x = 'Brusthöhendurchmesser (cm)', y = expression('Grundfläche (' * m^2 * ')')) + theme_minimal() ``` ### Boxplot ```{r} ggplot(data = df_clean, aes(x = factor(`Baumart`), y = `h [m]`, fill = factor(`Baumart`))) + geom_boxplot() + labs(title = 'Boxplot der Baumhöhen nach Baumart', x = 'Baumart', y = 'Höhe (m)', fill = 'Baumart') + scale_fill_manual(values = colors_tree_species) + theme_minimal() ``` ### Violinplot ```{r} ggplot(data = df_clean, aes(x = factor(`Baumart`), y = `h [m]`, fill = factor(`Baumart`))) + geom_violin() + labs(title = 'Boxplot der Baumhöhen nach Baumart', x = 'Baumart', y = 'Höhe (m)', fill = 'Baumart') + scale_fill_manual(values = colors_tree_species) + theme_minimal() ``` ### Karten ```{r} quadrants <- st_read('data/Geodaten_Moellergrab/EW_Marteloscop_AOIpolygons_32633.shp') quadrants ``` ```{r} st_crs(quadrants)$epsg ``` ```{r} ggplot(data = quadrants) + geom_sf() + geom_sf_text(aes(label = subID), size = 5) + theme_minimal() ``` ```{r} trees <- st_read('data/Geodaten_Moellergrab/EW_Marteloscop_TreeXY_32633.shp') trees ``` ```{r} st_crs(trees)$epsg ``` ```{r} ggplot(data = trees) + geom_sf(aes(color = spec_short, size = dbh_cm)) + scale_size(range = c(1, 3)) + labs(title = 'Baumstandororte mit Baumart und Brusthöhendurchmesser\nim Marteloskop Möllergrab', x = 'Easting (EPSG 32633)', y = 'Northing (EPSG 32633)', color = 'Baumart', size = "Brusthöhendurchmesser (cm)") + scale_color_manual(values = colors_tree_species) + theme_minimal() ``` ```{r} ggplot(data = quadrants) + geom_sf() + geom_sf(data = trees, aes(color = spec_short, size = dbh_cm, shape = factor(subID))) + scale_size(range = c(1, 3)) + labs(title = 'Baumstandorte mit Baumart und BHD im\nMarteloskop Möllergrab', x = 'Easting (EPSG 32633)', y = 'Northing (EPSG 32633)', color = 'Baumart', size = 'Brusthöhendurchmesser (cm)', shape = 'Quadrant', caption = "Datenquelle: Marteloskop 2021 | Autor: Jens Müller | Lizenz: CC BY-NC-SA 4.0") + scale_color_manual(values = colors_tree_species) + theme_minimal() + theme(plot.caption = element_text(hjust = 1, size = 7, face = "italic")) ``` ```{r} # save plot in PDF file ggsave("plot.pdf") ```