Climate Change Analysis

To study climate change, I used the data on the Combined Land-Surface Air and Sea-Surface Water Temperature Anomalies in the Northern Hemisphere from NASA’s Goddard Institute for Space Studies.

The tabular data of temperature anomalies can be found here

As a base period to define anomalies, I use the period between 1951-1980.

Let’s load the file:

weather <- 
  read_csv("https://data.giss.nasa.gov/gistemp/tabledata_v4/NH.Ts+dSST.csv", 
           skip = 1, 
           na = "***")

You have two objectives in this section:

  1. Select the year and the twelve month variables from the weather dataset. We do not need the others (J-D, D-N, DJF, etc.) for this assignment. Hint: use select() function.

  2. Convert the dataframe from wide to ‘long’ format. Hint: use gather() or pivot_longer() function. Name the new dataframe as tidyweather, name the variable containing the name of the month as month, and the temperature deviation values as delta.

I’m pivoting the weather data to long format for easier anlaysis.

tidyweather <- weather %>% 
               select(1:13) %>% 
               pivot_longer(!Year, names_to = "Month", values_to = "delta")

Next, I’ll the data using a time-series scatter plot, and add a trend line.

tidyweather <- tidyweather %>%
  mutate(date = ymd(paste(as.character(Year), Month, "1")),
         month = month(date, label=TRUE),
         year = year(date))

ggplot(tidyweather, aes(x=date, y = delta))+
  geom_point()+
  geom_smooth(color="red") +
  theme_bw() +
  labs ( 
    x = "Date",
    y = "Temperature deviation",
    title = "Weather Anomalies",
  ) 

It is sometimes useful to group data into different time periods to study historical data. For example, we often refer to decades such as 1970s, 1980s, 1990s etc. to refer to a period of time. NASA calcuialtes a temperature anomaly, as difference form the base periof of 1951-1980. The code below creates a new data frame called comparison that groups data in five time periods: 1881-1920, 1921-1950, 1951-1980, 1981-2010 and 2011-present.

I remove the data before 1800 using filter. Then, I use the mutate function to create a new variable interval which contains information on which period each observation belongs to.

comparison <- tidyweather %>% 
  filter(Year>= 1881) %>%     #remove years prior to 1881
  #create new variable 'interval', and assign values based on criteria below:
  mutate(interval = case_when(
    Year %in% c(1881:1920) ~ "1881-1920",
    Year %in% c(1921:1950) ~ "1921-1950",
    Year %in% c(1951:1980) ~ "1951-1980",
    Year %in% c(1981:2010) ~ "1981-2010",
    TRUE ~ "2011-present"
  ))

I’ll now create a density plot to study the distribution of monthly deviations (delta), grouped by the different time periods.

library(viridis)
ggplot(comparison, aes(x = delta, fill = interval)) +
  geom_density(alpha=.6) +
 # theme(legend.position="none") +
  theme_bw()+
  scale_fill_viridis(discrete = TRUE, option = "D") +
  labs(x = "Delta",
       y = "Density",
       title = "Distribution of monthly deviations (`delta`) grouped by the different time periods")

Let’s take a peek at average annual anomalies next:

#creating yearly averages
average_annual_anomaly <- tidyweather %>% 
  group_by(Year) %>%   #grouping data by Year
  
  # creating summaries for mean delta 
  # use `na.rm=TRUE` to eliminate NA (not available) values 
  summarise(mean_delta = mean(delta, na.rm = TRUE))

#plotting the data:
ggplot(average_annual_anomaly, aes(x = Year, y = mean_delta)) +
  geom_point() +
  #Fit the best fit line, using LOESS method
  geom_smooth(method = loess) +
  #change theme to theme_bw() to have white background + black frame around plot
  theme_bw() +
  labs(x = "Year", 
       y = "Mean tenperature deviation")