Skip to content

Instantly share code, notes, and snippets.

@padpadpadpad
Last active February 28, 2021 09:48
Show Gist options
  • Save padpadpadpad/60c568efc73c70c6b0874ab9ac123224 to your computer and use it in GitHub Desktop.
Save padpadpadpad/60c568efc73c70c6b0874ab9ac123224 to your computer and use it in GitHub Desktop.
Intro to plotting a time series in R and simple tidyverse functions
# good coding practice ####
# 1. #hashtag your code so you know what it does
# 2. clear workspace and load packages at the top to keep track of what you have loaded
# 3. make sure your working directory is in the right place
# 4. space things out in a way that makes your code readable to you
# 5. google things you do not understand. The answers are out there, go find them
# 6. do not get scared/angry when you get errors. It does get easier.... eventually
# clear workspace #### Good code practice to do first
# need to install mise first!
# removes all currently loaded packages, console, vars and figures from R
mise::mise(vars = TRUE, pkgs = TRUE, figs = TRUE, console = TRUE)
# set working directory - do not need to do here ####
#setwd("~/where/your/stuff/is")
# load packages ####
# if you do not have these packages - install.packages('package name')
library(dplyr)
library(tidyr)
library(ggplot2)
library(lubridate)
# create dummy data
# effect of Y (year) and M (month) on wave height and NAO
bY_wave <- 0.75
bM_wave <- 2
bY_nao <- 0.5
bM_nao <- 2.5
nao_start <- 0.05
wave_start <- 2
sd <- 0.5
# create dummy data of years and months ####
d <- data.frame(expand.grid(month = 1:12, year = 1970:2016)) %>%
mutate(., years_from_start = year - 1970)
# create dummy values for nao and wave height ####
d <- group_by(d, month, years_from_start, year) %>%
mutate(., wave_height = rnorm(1, wave_start, sd) + rnorm(1, bM_wave, sd)*month + rnorm(1, bY_wave, sd)*years_from_start,
nao = rnorm(1, nao_start, sd = sd) + rnorm(1, bM_nao, sd = sd)*month + rnorm(1, bY_nao, sd = sd)*years_from_start) %>%
data.frame() %>%
select(., -years_from_start)
# create a time column ####
# cannot have a time column without a day
# day will be the 1st of every month
# uses the package lubridate!
d <- mutate(d, time = ymd(paste(year, month, 1, sep = '-')))
# plot ####
# plot all of the data across years for NAO
ggplot(d) +
geom_point(aes(time, nao)) +
geom_smooth(aes(time, nao), se = FALSE) +
ylab('NAO index') +
xlab('Year')
# do the same for wave height
ggplot(d) +
geom_point(aes(time, wave_height)) +
geom_smooth(aes(time, wave_height), se = FALSE) +
ylab('NAO index') +
xlab('Year')
# plot nao vs wave height
ggplot(d) +
geom_point(aes(nao, wave_height)) +
ylab('Wave height') +
xlab('NAO index') +
theme_bw()
# create a column for timelag on waveheight ####
# months to give a time lag
# Time lag of one month
time_lag <- 1
d <- mutate(d, OneMon_lag = c(tail(wave_height, -time_lag), rep(mean(wave_height), times = time_lag)))
# Time lag of 5 months
time_lag <- 5
d <- mutate(d, FiveMon_lag = c(tail(wave_height, -time_lag), rep(mean(wave_height), times = time_lag)))
# plot ####
# time lag of wave_height vs NAO index
# plot nao vs wave height
ggplot(d) +
geom_point(aes(nao, FiveMon_lag, col = factor(month))) +
ylab('Wave height') +
xlab('NAO index') +
theme_bw() +
facet_wrap(~ month)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment