9.4 Density Plot Season
9.4.1 Goal
You want to create a density plot of a temperature series for each season of the year:
9.4.2 Data Basis
9.4.3 Solution
Create a new script, copy/paste the following code and run it:
library(ggplot2)
library(plotly)
library(dplyr)
# load time series data and aggregate daily mean values
library(dplyr)
library(lubridate)
library(redutils)
# read and print data
df <- read.csv("https://github.com/hslu-ige-laes/edar/raw/master/sampleData/flatTempHum.csv",
stringsAsFactors=FALSE,
sep =";")
# select temperature and remove empty cells
df <- df %>% select(time, FlatA_Temp) %>% na.omit()
colnames(df) <- c("time", "value")
df$season = getSeason(df$time)
# static chart with ggplot
plot <- ggplot(df) +
geom_density(aes(x = value, colour = season)) +
scale_color_manual(values=c("#440154", "#2db27d", "#fde725", "#365c8d")) +
ggtitle("Density Plot of Room Temperature for each season of the year") +
labs(x = "Temperature (\u00B0C)",
y = "Density (-)",
colour = "Legend") +
theme_minimal()
# interactive chart
plotly::ggplotly(plot)