STAA566_ggplot2_TStas

Author

Tiana Stastny

Load libraries

library(tidyverse)
-- Attaching packages --------------------------------------- tidyverse 1.3.1 --
v ggplot2 3.3.5     v purrr   0.3.4
v tibble  3.1.6     v dplyr   1.0.7
v tidyr   1.1.4     v stringr 1.4.0
v readr   2.1.1     v forcats 0.5.1
-- Conflicts ------------------------------------------ tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()
library(ggthemes)
Warning: package 'ggthemes' was built under R version 4.1.3
library(viridis)
Warning: package 'viridis' was built under R version 4.1.3
Loading required package: viridisLite

Obtain and load data

Data was downloaded from the AQUASTAT database. The National Rainfall Index (NRI) values were selected for the United States, along with several Southeast Asia countries, from approximately 1963 to 2019. Note that these values are in millimeters (mm). The plot below focuses on the values from 1970 to 1980.

# Load national rainfall data from AQUASTAT - Global water resources and uses
# https://tableau.apps.fao.org/views/ReviewDashboard-v1/country_dashboard?%3Aembed=y&%3AisGuestRedirectFromVizportal=y

national_rainfall_data <- read.csv("C:/Users/tiana/OneDrive/Documents/CSU_STAA_566_Data_Viz/rainfall.csv", header=TRUE)

# get into long format, put year as column variable
rainfall <- national_rainfall_data %>%
  pivot_longer(
    cols = starts_with("X"),
    names_to = "year",
    names_prefix = "yr",
    values_to = "amount",
  )

Select subset of data to plot

# There are two types of sources - imputed and external.  There are more values under "imputed", so we'll look at these.
all <- rainfall %>% filter(Symbol == "I  ")

# remove 'X' from year values
all <- all %>%                              
  mutate(year = substring(year, 2))

# get subset of data from 1970 to 1980
subset <- all %>% filter(year >= "1970", year < "1981")

# convert the rainfall amount values to 'numeric' type
subset$amount <- as.numeric(gsub(",", "", subset$amount))

Build ggplot2 object

# build ggplot
p_gm <- ggplot(data = subset,
            mapping = aes(x = year,
                          y = amount, 
                          group = Country, 
                          color = Country
                          )
            )
rainfall_plot <- p_gm + 
  geom_line(data=subset[!is.na(subset$amount),]) +  # connect lines over NAs
  geom_point(data=subset[!is.na(subset$amount),]) + # points if not NA
  theme_classic() + 
  theme(axis.text.x = element_text(angle = 45, hjust=1)) +
  scale_color_viridis(discrete = TRUE) + 
  xlab("Year") + 
  ylab("Amount of rain (mm)") + 
  ggtitle("Amount of rain per year in Southeast Asia vs United States")

Save Figure as pdf

pdf("rainfall.pdf", heigh=6, width=8)
print(rainfall_plot)
dev.off()
png 
  2 

Display plot

rainfall_plot