Skip to content

Commit

Permalink
Prep for 0.3.1 release
Browse files Browse the repository at this point in the history
  • Loading branch information
lmullen committed Mar 29, 2018
1 parent 8eca48d commit 17b7487
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 67 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: USAboundaries
Type: Package
Title: Historical and Contemporary Boundaries of the United States of America
Version: 0.3.0.9000
Date: 2017-07-05
Version: 0.3.1
Date: 2018-03-29
Authors@R: c(person("Lincoln", "Mullen",
email = "[email protected]",
role = c("aut", "cre"),
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Additions and clarifications to documentation following @AndySouth's suggestions for JOSS peer review (#38).
- `us_cities()` now returns an `sf` object rather than a data frame (#36).
- `us_cities()` gains a `states` argument to match other functions in the package (#35).
- Citation to JOSS paper.

# USAboundaries 0.3.0

Expand Down
22 changes: 12 additions & 10 deletions R/state_plane.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@
#' projection data frame, see \code{\link{state_proj}}.
#'
#' @examples
#' state_plane(state = "MA", type = "epsg")
#' state_plane(state = "MA", type = "proj4")
#' state_plane(state = "MA", plane_id = "island", type = "epsg")
#' state_plane(state = "MA", plane_id = "island", type = "proj4")
#' if (require(USAboundariesData)) {
#' state_plane(state = "MA", type = "epsg")
#' state_plane(state = "MA", type = "proj4")
#' state_plane(state = "MA", plane_id = "island", type = "epsg")
#' state_plane(state = "MA", plane_id = "island", type = "proj4")
#'
#' # Show the difference made by a state plane projection
#' if (require(sf)) {
#' va <- us_states(states = "VA", resolution = "high")
#' plot(st_geometry(va), graticule = TRUE)
#' va <- st_transform(va, state_plane("VA"))
#' plot(st_geometry(va), graticule = TRUE)
#' # Show the difference made by a state plane projection
#' if (require(sf)) {
#' va <- us_states(states = "VA", resolution = "high")
#' plot(st_geometry(va), graticule = TRUE)
#' va <- st_transform(va, state_plane("VA"))
#' plot(st_geometry(va), graticule = TRUE)
#' }
#' }
#'
#' @export
Expand Down
12 changes: 5 additions & 7 deletions cran-comments.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
This is an updated version of the 'USAboundaries' package from version 0.2.0 to version 0.3.0.

Following a suggestion from CRAN maintainers, documentation links to the USAboundariesData package have been removed.
This is a minor release of the 'USAboundaries' package. It makes all of the included data have a consistent output format.

## Test environments

* local OS X install, R 3.4.0
* ubuntu 14.04 (on travis-ci), R-devel, R-current, R-oldrel
* win-builder (R-devel and R-release)
* local OS X install, R-release
* Travis-CI, R-devel, R-release, R-oldrel
* ubuntu 14.04 (on travis-ci), R-devel, R-release, R-oldrel
* win-builder (R-devel)

## R CMD check results

There were no ERRORs or WARNINGs.

There are several NOTEs about suggested package in an additional repository which adds extra data. This 'USAboundariesData' package is provided in the rOpenSci CRAN-style repository, and it is installed on user request.

22 changes: 12 additions & 10 deletions man/state_plane.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 53 additions & 38 deletions vignettes/usaboundaries-sample-analysis.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -24,79 +24,94 @@ library(USAboundaries)
library(dplyr)
library(sf)
library(leaflet)
# Check that USAboundariesData is available, since it is not on CRAN
avail <- requireNamespace("USAboundariesData", quietly = TRUE)
```

We can load the voting data for the 115th Congress into `md_votes`. We use this package to get the contemporary (2016) boundaries for Congressional districts in Maryland. They are returned as an `sf` object from the [sf](https://github.com/r-spatial/sf/) package.

```{r}
md_votes <- read.csv(system.file("extdata", "md-115.csv", package = "USAboundaries"),
colClasses = c("character", "integer", "character", "integer",
"character", "integer", "integer", "integer",
"integer", "integer", "integer", "numeric",
"numeric", "numeric", "numeric", "numeric"))
md_districts <- us_congressional(states = "MD")
if (avail) {
md_votes <- read.csv(system.file("extdata", "md-115.csv",
package = "USAboundaries"),
colClasses = c("character", "integer", "character", "integer",
"character", "integer", "integer", "integer",
"integer", "integer", "integer", "numeric",
"numeric", "numeric", "numeric", "numeric"))
md_districts <- us_congressional(states = "MD")
}
```

In order to map the data, we have to join the voting data to the spatial data.

```{r}
md_115 <- md_districts %>%
left_join(md_votes, by = c("cd115fp" = "district"))
if (avail) {
md_115 <- md_districts %>%
left_join(md_votes, by = c("cd115fp" = "district"))
}
```

The data has the percentage of votes for Democrats and Republicans. We are going to map the margin that Republicans had over an even vote, so that we can see both the Democrat and Republican results. For a more careful analysis we would have to take account of third parties and write-in votes, but this will serve our purpose.

```{r}
md_115$margin_percent <- md_115$percentage_republican - 0.5
if (avail) {
md_115$margin_percent <- md_115$percentage_republican - 0.5
}
```

We create a color palette to turn the margin of victory into red and blue colors.

```{r}
palette <- colorBin("RdBu", domain = c(-0.3, 0.3), bins = 7, reverse = TRUE)
if (avail) {
palette <- colorBin("RdBu", domain = c(-0.3, 0.3), bins = 7, reverse = TRUE)
}
```

We can use the [leaflet](https://rstudio.github.io/leaflet/) package to make the map.

```{r}
leaflet(md_115) %>%
addTiles() %>%
addPolygons(color = "black",
opacity = 1,
weight = 1,
fillColor = ~palette(margin_percent),
fillOpacity = 1,
label = ~paste0("District: ", cd115fp))
if (avail) {
leaflet(md_115) %>%
addTiles() %>%
addPolygons(color = "black",
opacity = 1,
weight = 1,
fillColor = ~palette(margin_percent),
fillOpacity = 1,
label = ~paste0("District: ", cd115fp))
}
```

We can do the same analysis for the 3rd Congress, thanks to the historical data included in the package. In this case, we will have to map counties rather than Congressional districts. The parties of interest are the Federalists and the Democratic-Republicans.

We start by loading and joining the data.

```{r}
md_03_votes <- read.csv(system.file("extdata", "md-003.csv", package = "USAboundaries"),
stringsAsFactors = FALSE)
md_03_districts <- us_counties(map_date = "1792-10-01",
resolution = "high", states = "MD")
md_003 <- md_03_districts %>%
left_join(md_03_votes, by = c("id" = "county_ahcb"))
md_003$margin_percent <- md_003$federalist_percentage - 0.5
if (avail) {
md_03_votes <- read.csv(system.file("extdata", "md-003.csv",
package = "USAboundaries"),
stringsAsFactors = FALSE)
md_03_districts <- us_counties(map_date = "1792-10-01",
resolution = "high", states = "MD")
md_003 <- md_03_districts %>%
left_join(md_03_votes, by = c("id" = "county_ahcb"))
md_003$margin_percent <- md_003$federalist_percentage - 0.5
}
```

Then we create a new palette function and make the map.

```{r}
palette_03 <- colorBin("PRGn", domain = c(-0.5, 0.5), bins = 9)
leaflet(md_003) %>%
addTiles() %>%
addPolygons(color = "black",
opacity = 1,
weight = 1,
fillColor = ~palette_03(margin_percent),
fillOpacity = 1,
label = ~paste0("County: ", name))
if (avail) {
palette_03 <- colorBin("PRGn", domain = c(-0.5, 0.5), bins = 9)
leaflet(md_003) %>%
addTiles() %>%
addPolygons(color = "black",
opacity = 1,
weight = 1,
fillColor = ~palette_03(margin_percent),
fillOpacity = 1,
label = ~paste0("County: ", name))
}
```




0 comments on commit 17b7487

Please sign in to comment.