Here was our initial wrangle of the data:
ZipGeography %>%
group_by(State) %>%
summarise(area = sum(LandArea,
na.rm = TRUE),
population = sum(Population,
na.rm = TRUE)) %>%
mutate(Density = (population / area))Look on the next tab for the data table. You will notice that Washington, DC is listed among the states. Since it is entirely an urban area, its population density (about 3596) is very large in comparison to the states.
Modify the wrangling so that no density is allowed to be more than a fixed value. Since New Jersey is the state with the highest population density at around 458 people per unit area, let’s set 500 as the maximum possible density. This can be accomplished with a call to the pmin() function, which computes pairwise minima of two given variables.
Now DC will have a density of 500.
ZipGeography %>%
group_by(State) %>%
summarise(area = sum(LandArea,
na.rm = TRUE),
population = sum(Population,
na.rm = TRUE)) %>%
mutate(Density = (population / area)) %>%
mutate(Density = pmin(Density, 500)) %>%
USMap(key = State, fill = Density)---
title: "The Population Density Problem"
author: "Homer White"
date: "9/14/2017"
output:
flexdashboard::flex_dashboard:
orientation: columns
source_code: embed
---
```{r include = FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(DataComputing)
```
# Diagnosis
Intro { .sidebar }
---------------------------
### Topic
In class we had a bit of trouble with the third part of Problem 12 in Chapter 7: the choropleth map of population density for U.S. states appeared not to vary in color by state.
This dashboard diagnoses the problem and suggests a solution.
### Package Note
To display the table nicely, we used the `datatable()` function from the **DT** package. Consult the source code to see how this works.
Column { .tabset }
------------------
### First Wrangle
Here was our initial wrangle of the data:
```{r eval = FALSE}
ZipGeography %>%
group_by(State) %>%
summarise(area = sum(LandArea,
na.rm = TRUE),
population = sum(Population,
na.rm = TRUE)) %>%
mutate(Density = (population / area))
```
Look on the next tab for the data table. You will notice that Washington, DC is listed among the states. Since it is entirely an urban area, its population density (about 3596) is *very* large in comparison to the states.
### The Table
```{r echo = FALSE}
ZipGeography %>%
group_by(State) %>%
summarise(area = sum(LandArea,
na.rm = TRUE),
population = sum(Population,
na.rm = TRUE)) %>%
mutate(Density = (population / area)) %>%
DT::datatable(rownames = FALSE)
```
Column { .tabset }
------------------
### New Wrangle
Modify the wrangling so that no density is allowed to be more than a fixed value. Since New Jersey is the state with the highest population density at around 458 people per unit area, let's set 500 as the maximum possible density. This can be accomplished with a call to the `pmin()` function, which computes pairwise minima of two given variables.
Now DC will have a density of 500.
```{r eval = F}
ZipGeography %>%
group_by(State) %>%
summarise(area = sum(LandArea,
na.rm = TRUE),
population = sum(Population,
na.rm = TRUE)) %>%
mutate(Density = (population / area)) %>%
mutate(Density = pmin(Density, 500)) %>%
USMap(key = State, fill = Density)
```
### The Graph
```{r echo = F, fig.width=5, fig,height=4}
ZipGeography %>%
group_by(State) %>%
summarise(area = sum(LandArea,
na.rm = TRUE),
population = sum(Population,
na.rm = TRUE)) %>%
mutate(Density = (population / area)) %>%
mutate(Density = pmin(Density, 500)) %>%
USMap(key = State, fill = Density)
```