Skip to content

Instantly share code, notes, and snippets.

View robwschlegel's full-sized avatar

Robert William Schlegel robwschlegel

View GitHub Profile
# First load libraries
library(tidyverse)
library(scales)
# Then data
load('~/blog/data/ERA_pixel.Rdata')
wind_scale <- 1
y_axis <- seq(-5, 5, 5)
## Assuming your data frame is called 'my_df'
# First it is necessary to correct any bearings of '0' to '360'
my_df$bearing[my_df$bearing == 0] <- 360
my_df$u <- (1 * my_df$speed) * sin((my_df$bearing * pi / 180.0))
my_df$v <- (1 * my_df$speed) * cos((my_df$bearing * pi / 180.0))
ggplot(data = ERA_pixel, aes(x = date, y = y_axis)) +
# Here we create the wind vectors as a series of segments with arrow tips
geom_segment(aes(x = date, xend = date + u*wind_scale, y = 0, yend = v*wind_scale),
arrow = arrow(length = unit(0.15, 'cm')), size = 0.5, alpha = 0.7) +
# I think adding points at the base of the vectors makes the figure easier to read
geom_point(aes(x = date, y = 0), alpha = 0.5, size = 1) +
# Changing the dates to better match the range shown on the x axis
scale_x_date(labels = date_format('%Y-%m-%d'), breaks = date_breaks('4 days')) +
# Change the y axis labels to make sense
scale_y_continuous(breaks = y_axis, labels = as.character(abs(y_axis)/wind_scale)) +
# Reset the wind scalar to be half the size
wind_scale <- 0.5
# The exact same code as above
ggplot(data = ERA_pixel, aes(x = date, y = y_axis)) +
geom_segment(aes(x = date, xend = date + u, y = 0, yend = v),
arrow = arrow(length = unit(0.15, 'cm')), size = 0.5, alpha = 0.7) +
geom_point(aes(x = date, y = 0), alpha = 0.5, size = 1) +
scale_x_date(labels = date_format('%Y-%m-%d'), breaks = date_breaks('4 days')) +
scale_y_continuous(breaks = y_axis/wind_scale, labels = as.character(abs(y_axis)/wind_scale)) +
# Reset the wind scale to 1
wind_scale <- 1
# Now we use temperature for the y axis, so some things must change
ggplot(data = ERA_pixel, aes(x = date, y = temp)) +
geom_line(colour = 'salmon', show.legend = F, size = 1.5) +
geom_segment(aes(x = date, xend = date + u,
y = mean(temp, na.rm = T), yend = mean(temp, na.rm = T) +v),
arrow = arrow(length = unit(0.15, 'cm')), size = 0.5, alpha = 0.7) +
geom_point(aes(x = date, y = mean(temp, na.rm = T)), alpha = 0.5, size = 1) +
## Load libraries
library(tidyverse)
library(broom)
# library(RColorBrewer)
## Load data
# Educational Inequality Gini Coefficient (after age 15)
education_inequality <- read.csv("../data/EducationalInequalityGiniCoefficient.csv")
education_inequality$variable <- "education_inequality"
# GDP per Capita
# Here we run linear models on all available data against GDP per capita
# We are not controlling for country or year
lm_data_all <- data_long %>%
left_join(., gdp_per_capita[,-5], by = c("ccode", "country.name", "year")) %>%
rename(., value = value.x, gdp = value.y) %>%
group_by(variable) %>%
mutate(n = sum(!is.na(gdp))) %>%
ungroup() %>%
nest(-n, -variable) %>%
mutate(fit = map(data, ~ lm(value ~ gdp, data = .)),
# Or we may control for years of sampling
# This allows us to see if the relationship changes much over time
lm_data_year <- data_long %>%
left_join(., gdp_per_capita[,-5], by = c("ccode", "country.name", "year")) %>%
rename(., value = value.x, gdp = value.y) %>%
group_by(year, variable) %>%
mutate(n = sum(!is.na(gdp))) %>%
ungroup() %>%
filter(n > 10) %>% # Require at least 10 data points
nest(-n, -year, -variable) %>%
# Or we may control for the country of sampling
# This allows us to see if these relationships
# differ in certain parts of the world
lm_data_country <- data_long %>%
left_join(., gdp_per_capita[,-5], by = c("ccode", "country.name", "year")) %>%
rename(., value = value.x, gdp = value.y) %>%
group_by(country.name, variable) %>%
mutate(n = sum(!is.na(gdp))) %>%
ungroup() %>%
filter(n > 10) %>% # Require at least 10 data points