Time Series, Maps, Interactive Graphs

Rob Kabacoff
Saturday, April 23, 2022

Time Dependent Graphs

  • line graphs
  • area charts
  • stacked area charts
  • dumbell and slope graphs (not covered)

Time series

Convert character values to dates

example

x <- c("10/2/2008", "12/5/2008")
xDate <- as.Date(x, format="m/d/Y")

see https://www.statmethods.net/input/dates.html

Time series plot

library(ggplot2)
ggplot(data, aes(x = datevar, y = )) +
  geom_line() 

customize x-axis using

+ scale_x_date(date_breaks = , labels =)

Area chart

ggplot(data, aes(x = datevar, y = )) +
  geom_area(fill=, color=)

Stacked area chart

ggplot(data, aes(x = datevar, y = , fill=)) +
  geom_area(color=)

Interactive Graphs

Making graphs interactive

  • ggplot2 graphs are static by nature

  • any ggplot2 graph can be made interactive via plotly

Steps

  • create ggplot2 graph
  • save it as an object
  • process it with the ggplotly function

Interactive Example

library(ggplot2)
library(plotly)
p <- ggplot(economics, 
            aes(x = date, y = psavert)) +
  geom_line() 
ggplotly(p)

Maps

  • dot density
  • choropleth

Dot density map

Each observation should include longitude and latitude

Adding points to a map

library(leaflet)

leaflet() %>% 
  addProviderTiles(providers$namehere) %>%
  setView(lng = , lat = , zoom = ) %>%
  addCircles(lng =, lat =, popup =, color =)

type providers at the console to see provider names

Choropleth map

Easy choropleth maps with the choroplethr package by Ari Lamstein

  • country_choropleth()
  • state_choropleth()
  • county-chorpleth()
  • zip_chorpleth()

Resulting maps are ggplot2 graphs and can be further modified.

Country choropleth maps

library(chorplethr)
country_choropleth(data, "title", num_colors = , zoom = )
  • data should have two columns (region and value)
  • Elements in the “region” column must exactly match how regions are named in the “region” column in ?country.map.
  • num_colors - number of colors used in map (0-9) with 0 for divergent scale and 1 for continuous scale.
  • zoom is a character vector listing countries

US state choropleth maps

library(chorplethr)
state_choropleth(data, "title", num_colors = , zoom = )
  • data should have two columns (region and value)
  • Elements in the “region” column must exactly match how regions are named in the “region” column in ?state.map.
  • num_colors - number of colors used in map (0-9) with 0 for divergent scale and 1 for continuous scale.
  • zoom is a character vector listing states

US counties choropleth maps

library(chorplethr)
county_choropleth(data, "title", num_colors = , 
                  state_zoom = , county_zoom = )
  • data should have two columns (region and value)
  • Elements in the “region” column must exactly match how regions are named in the “region” column in ?county.map.
  • num_colors - number of colors used in map (0-9) with 0 for divergent scale and 1 for continuous scale.
  • state_zoom and county_zoom are state and county vectors

Changing map colors

modify choroplethr graph with ggplot2 functions

example

library(chorplethr)
county_choropleth(data, "title", num_colors = , 
                  state_zoom = , county_zoom = ) +
  scale_fill_brewer(palette = "Set1")

see https://r-graph-gallery.com/38-rcolorbrewers-palettes.html