Ex05: German Climate

Download Exercise (Right Click - Save Link as…)

The German Weather Service DWD provides a variety of openly available climate, weather and phenology data in their data portal.

  • Download the regional average summer temperature of germany.
  • The file is a simple .txt file. Open it with a text editor and have a conscious look at the structure and content.
  • Load the file into R with the read.table or read.csv function.
  • By default, read.table will throw an error at you and the output of read.csv is weird. Why?
  • Look at the help page of read.csv / read.table and fix the issues.

Hint: the correct data.frame should have 143 observations of 20 variable that look something like this:

Jahr summer Brandenburg.Berlin Brandenburg Baden.Wuerttemberg Bayern Hessen Mecklenburg.Vorpommern Niedersachsen Niedersachsen.Hamburg.Bremen Nordrhein.Westfalen Rheinland.Pfalz Schleswig.Holstein Saarland Sachsen Sachsen.Anhalt Thueringen.Sachsen.Anhalt Thueringen Deutschland X
1881 summer 17.13 17.11 17.03 16.39 16.62 16.12 16.24 16.24 16.66 16.90 15.80 17.19 16.20 16.88 16.46 15.95 16.53 NA
1882 summer 16.45 16.44 14.85 14.46 15.02 16.19 15.71 15.72 15.48 15.16 16.10 15.33 15.05 16.02 15.41 14.64 15.33 NA
1883 summer 17.52 17.51 15.87 15.50 16.15 16.66 16.35 16.36 16.37 16.23 16.31 16.42 16.49 17.14 16.57 15.84 16.26 NA
1884 summer 17.00 16.98 16.08 15.35 16.01 16.32 16.20 16.21 16.45 16.52 16.19 16.83 15.92 16.57 16.02 15.33 16.10 NA
1885 summer 17.18 17.17 16.63 16.05 15.84 16.06 15.78 15.78 16.11 16.28 15.53 16.65 16.29 16.63 16.10 15.43 16.18 NA
1886 summer 17.16 17.15 16.04 15.47 15.88 16.05 15.89 15.89 16.17 16.17 15.52 16.42 16.09 16.61 16.05 15.35 16.00 NA
  • Which year had the warmest summer in Bayern since the beginning of the recordings?
  • What was the average summer temperature in Nordrhein-Westfalen since the year 2000.
  • Which German state had the coldest summer in the year 2005.

Pivots

  • Create a subset that only contains the columns Jahr, Thueringen and Sachsen.Anhalt.

  • Convert this data.frame to the long format with tidyr::pivot_longer with three columns: “Jahr”, “Bundesland” and “Temperatur”.

  • Compare the temperature of Thueringen and Sachsen.Anhalt:

  • Use a boxplot to visualize both state’s summer temperatures.

  • Use a t.test to check if there are significant differences between the summer temperatures of both states?

  • Hint: for these tasks you need the formula notation y ~ x.

Climate Change in Germany

Now we want to have a look at the temperature anomaly over the years, i.e. the deviation of the temperature from a long term mean. For this we want to re-create a figure like this:

  • Calculate the average summer temperature of Germany between the years 1961 and 1990.
  • Calculate the temperature anomaly for each year in Germany, i.e. the deviation of the yearly temperature to the calculated long term average. Save this anomaly information in a new column in the data.frame.
  • Create a first plot of the anomaly per year. Set the argument type = "h" in the plot function to get bars instead of points.
  • To get the some color in the plot, we need to encode the anomaly information into groups. To do this, create a new column that contains the word “blue” for years with a negative temperature anomaly and the word “red” for positive anomalies. Hint: You can do this in two lines of code without any function, or in one line of code with the ifelse function.
  • Create the plot again, but this time set the argument col (which stands for color) to your new column that contains the color information.