knitr::opts_chunk$set( warning = FALSE,
                       message = FALSE,
                       collapse = TRUE,
                       comment = "#>"
)


devtools::load_all()
library(ggplot2)
library(dplyr)
library(tidyVA)

tidyVA includes simple features tibbles of VHA geographies.

For this example we'll use the submarket dataset. This is a tidy dataframe that includes simple features geometry for VHA submarket regions. data.va.gov

data("submarket")

ggplot() + 
  geom_sf(data=submarket, fill = NA)

Although accurate, this output is not very easy to work with. But, since this is now in tidy format, we can easily filter and manipulate the data. The function shift_geo() in tidyVA will project and repositon Alaska and Hawaii.

shift_geo(submarket) %>% 
  ggplot() + 
  geom_sf(fill = NA) +
  theme_minimal()

If you create an R object, this object can be mapped multiple times as individual layers representing different attributes:

submarket <- shift_geo(submarket)

ggplot() + 
  geom_sf(data = submarket,           
          aes(fill = VISN), color = NA) +  
  geom_sf(data = submarket, fill = NA, 
          color = "#ffffff", size = .5) + 
  theme_minimal() + 
  theme(legend.position = "bottom") +
  scale_fill_viridis_d()

Alternatively, you can aggregate this dataset to the VISN level, and use as needed. Keep in mind that VHA submarkets in the Caribbean and the Pacific Ocean, are not included - thus, VISNs 21 and 8 are not fully represented.

visn <- submarket %>% 
  group_by(VISN) %>% 
  summarise()

ggplot() + 
  geom_sf(data = visn, aes(fill = VISN), 
          show.legend = FALSE) +
  theme_minimal() + 
  scale_fill_viridis_d(option = "cividis")

Spatial intersection

A major limitation of the original data is that attributes were present based on a geography's hierachy. The county dataset has VHA attributes in addition to State and County attributes. The visn dataset only has a VISN attribute:

data("states")
data("visn")

glimpse(visn)

With simple features we can easily perform geometric operations and gather attributes based on a spatial relationship:

visn_st <- sf::st_intersection(states, visn)

glimpse(visn_st)

We can now filter this dataframe by VISN and keep State attributes

visn_st %>% 
  filter(VISN %in% c("01","02")) %>% 
  ggplot() + 
  geom_sf(aes(fill = VISN), color = "#ffffff") +
  scale_fill_viridis_d(option = "cividis") +
  theme_va()

Or by State, and keeping VISN attributes. New York for example is mostly within VISN 2, but, there are small catchments areas in the Western and Southern parts of the state that are outside VISN 2.

visn_st %>% 
  filter(STUSPS %in% c("NY","CT","VT","ME", "MA","NH")) %>% 
  ggplot() + 
  geom_sf(aes(fill = NAME)) +
  scale_fill_viridis_d() +
  theme_va()

Theme Function

tidyVA also includes a ggplot theme: theme_va().

This theme has several possible parameters. The core of this function is theme_ipsum from hrbrthemes, but with some changes and set defaults.

data("visn")

visn %>% 
  ggplot(aes(reorder(VISN,Shape_Area), Shape_Area)) +
  geom_col() + 
  labs(x = "VISN", y = "Shape Area", 
       caption = "CRS - EPSG:4326") +
  ggtitle("Shape Area of VA Service Networks", 
          subtitle = "This is an example plot") +
  theme_va(grid = "Y")

Map Theme

When plotting maps, you may want to remove the axis text from geom_sf(). For this, the map argument of theme_va() can be set to TRUE.

Additionally, setting the grid_col argument to "transparent" will remove panel grid lines from the plot.

visn_st %>% 
  filter(STUSPS %in% c("NY","CT","VT","ME", "MA","NH")) %>% 
  ggplot() + 
  geom_sf(aes(fill = NAME)) +
  scale_fill_viridis_d(option = "cividis") +
  labs(caption = "Caption wil go here", fill = "") +
  ggtitle("Example Plot for Maps", 
          subtitle = "Cividis Palette goes well with VA theme") +
  theme_va(map = TRUE, grid_col = "transparent") 


iecastro/tidyVA documentation built on Jan. 13, 2020, 1:06 p.m.