library(dplyr)
library(ggplot2)
library(plotly)
Dynamic Graph
Description
I downloaded corn yield data from USDA’s quick stats lite site so that I could compare corn yields for irrigated land over time from a couple of states of interest: Montana (where I will be moving to) and Colorado (because of CSU).
To replicate the data one can visit the site linked above and apply the filters described below:
- Sector = CROPS
- Group = FIELD CROPS
- Commodity = CORN
- View = Acreage, Yield, and Production - Irrigated / Non-Irrigated
- Year = 1950-2022
- Geographic Level = State
Note: I exported data for all states, but had to do it in two exports. The tool provided an empty CSV when I attempted to select data for all states at once. This data is also available in this GitHub repo.
Load Data
# Read data from a csv file
# data <- read.csv('corn_production.csv')
<- read.csv('corn_production1.csv')
data1 <- read.csv('corn_production2.csv')
data2 <- bind_rows(data1, data2)
data
# Change the names to lower case so that they're easier for me to work with
names(data) <- tolower(names(data))
Data preparation
Here I will rename, select a subset of columns, and filter the data to records that have data for production as well as those where the corn was grown on irrigated land.
# Rename, select a subset of columns, and filter
<- data %>%
data rename(
production=production.in.bu,
harvested_area=area.harvested.in.acres,
yield=yield.in.bu...acre
%>%
) filter(
> 0,
production == 'IRRIGATED'
prodn.practice %>%
) select(year, location, prodn.practice, harvested_area, production, yield)
Plotting
In the plot below I compare the corn yields of Colorado and Montana as was done previously. This time the plot is dynamic of course, and was built with plotly. The tooltip was also set to display yields for all of the locations so that the yields for Colorado and Montana for a specific time could be viewed more easily. The colors were also chosen to make these states easy to compare to the other corn producing states in the country.
<- c('COLORADO', 'MONTANA')
aoi <- data %>% filter(!location %in% aoi)
background_areas <- data %>% filter(location == 'COLORADO')
colorado <- data %>% filter(location == 'MONTANA')
montana
<- plot_ly(
plotly_figure
background_areas, x=~year,
y=~yield,
split=~location,
color=I('grey'),
alpha=1.0,
type='scatter',
mode='lines',
showlegend=FALSE
%>%
) add_trace(
data=colorado,
x=~year,
y=~yield,
color=~location,
line=list(color='red'),
type='scatter',
mode='lines',
showlegend=TRUE
%>%
) add_trace(
data=montana,
x=~year,
y=~yield,
color=~location,
line=list(color='blue'),
type='scatter',
mode='lines',
showlegend=TRUE
%>%
) layout(hovermode='x unified')
plotly_figure