<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/weighted-population-density/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.decisionsciencenews.com/2017/06/26/weighted-population-density/"> R – Decision Science News</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>DENSITY THE AVERAGE PERSON EXPERIENCES</p>
<p>
<a rel="nofollow" href="https://i0.wp.com/www.decisionsciencenews.com/wp-content/uploads/2017/06/state_v_unweighted_and_weighted_density.png" target="_blank"><img sizes="(max-width: 485px) 100vw, 485px" src="https://i2.wp.com/www.decisionsciencenews.com/wp-content/uploads/2017/06/state_v_unweighted_and_weighted_density.s.png?w=450" alt="" class="wp-image-6202" data-recalc-dims="1"></a><br>
<b><i>CLICK TO ENLARGE</i></b>
</p>
<p>In Alaska, there’s about one person for each square mile of land.</p>
<p>You might picture a typical Alaskan not being able to see the next house.</p>
<p>But it’s not that way of course.</p>
<p>Most of Alaska is uninhabited. People have crowded into a few areas. </p>
<p>The average Alaskan experiences a population density of about 72 people per square mile. </p>
<p>That’s a lot more than one.</p>
<p>In the <a rel="nofollow" href="https://www.r-project.org/" target="_blank">R</a> code below, we roughly estimate the weighted population density for each US state, that is, the population density that the average person experiences. We do this for each state by taking the average of its counties’ population densities, weighted by the population of each county. It would be even better to do this for smaller areas, such as census tracts, but we were too lazy to chase down the data.</p>
<p>In the figure at the top of this post, we see the weighted and unweighted population densities for each state.</p>
<p>Note how New Jersey has a higher population density than New York, but when you look at what the average person experiences, it flips.</p>
<p>Amazingly, the average person in New York State shares a square mile with more than 10,000 other residents!</p>
<p>What states have the biggest ratios of weighted to unweighted densities? The chart below shows states with a 10x or greater ratio in blue.</p>
<p>
<a rel="nofollow" href="https://i0.wp.com/www.decisionsciencenews.com/wp-content/uploads/2017/06/unweighted_v_weighted_density.png" target="_blank"><img sizes="(max-width: 485px) 100vw, 485px" src="https://i2.wp.com/www.decisionsciencenews.com/wp-content/uploads/2017/06/unweighted_v_weighted_density.s.png?w=450" alt="" class="wp-image-6204" data-recalc-dims="1"></a></p>
<p><b><i>CLICK TO ENLARGE</i></b>
</p>
<p>Here are the states with the biggest ratios:</p>
<p>Alaska – The average person experiences a population density that is 62 times greater than the state’s density.<br>
New York – 39 times<br>
Nebraska – 24 times<br>
Utah – 21 times<br>
Colorado – 16 times<br>
Minnesota – 14 times<br>
Oregon – 13 times<br>
New Mexico – 12 times<br>
Kansas – 12 times<br>
Texas – 12 times<br>
Illinois – 12 times</p>
<p>R CODE FOR THOSE WHO WISH TO FOLLOW ALONG AT HOME<br>
<code><br>
library(tidyverse)<br>
library(ggrepel)<br>
library(scales)<br>
setwd(&quot;C:/Dropbox/Projects/20170605_Population_Density&quot;)<br>
#source https://factfinder.census.gov/bkmk/table/1.0/en/DEC/10_SF1/GCTPH1.US05PR<br>
df &lt;- read_csv(&quot;DEC_10_SF1_GCTPH1.US05PR.csv&quot;, skip = 1)
names(df) = c(
  &quot;id&quot;,
  &quot;state&quot;,
  &quot;country&quot;,
  &quot;geo_id&quot;,
  &quot;geo_id_suffix&quot;,
  &quot;geographic_area&quot;,
  &quot;county_name&quot;,
  &quot;population&quot;,
  &quot;housing_units&quot;,
  &quot;total_area&quot;,
  &quot;water_area&quot;,
  &quot;land_area&quot;,
  &quot;density_population_sqmi_land&quot;,
  &quot;density_housing_units_sqmi_land&quot;
)
#drop puerto rico and DC. sorry guys!
df = df %>%<br>
  filter(geo_id != &quot;0400000US72&quot;) %>%<br>
  filter(geo_id != &quot;0500000US11001&quot;) %>%<br>
  filter(geo_id != &quot;0400000US11&quot;)<br>
#make a state data frame with just four facts for each state (for later joining)<br>
sdf = df %>%<br>
  filter(!is.na(geo_id_suffix)) %>%<br>
  filter(stringr::str_length(geo_id_suffix) &lt; 5) %>% #states have short geoids<br>
  mutate(<br>
    state = stringr::str_sub(geo_id_suffix, 1, 2),<br>
    geographic_area = stringr::str_sub(geographic_area, 16, stringr::str_length(geographic_area))<br>
  ) %>%<br>
  select(state,<br>
         geographic_area,<br>
         population,<br>
         density_population_sqmi_land)<br>
names(sdf) = c(&quot;state&quot;, &quot;geographic_area&quot;, &quot;state_pop&quot;, &quot;state_density&quot;)<br>
#clean up county data, dropping irrelevant cols<br>
df = df %>%<br>
  filter(!is.na(geo_id_suffix)) %>%<br>
  filter(stringr::str_length(geo_id_suffix) == 5) %>% #counties have geoids of length 5<br>
  mutate(state = stringr::str_sub(geo_id_suffix, 1, 2)) %>%<br>
  select( #drop unneeded columns<br>
    -id,-country,-geo_id,-housing_units,-total_area,<br>
    -water_area,-density_housing_units_sqmi_land<br>
  )<br>
#join the state data with the county data<br>
result = left_join(df, sdf, by = &quot;state&quot;) %>%<br>
  group_by(state) %>%<br>
  summarise(weighted_density = round(sum(<br>
    population / state_pop * density_population_sqmi_land<br>
  ), 0)) %>%<br>
  ungroup() %>%<br>
  left_join(sdf, .) %>%<br>
  arrange(-weighted_density) %>%<br>
  #mark states with weighted density 10x higher than unweighted density<br>
  mutate(highlight = weighted_density / state_density > 10)<br>
write_csv(result, &quot;result.csv&quot;)<br>
#graphit, Schulte style<br>
p = ggplot(result,<br>
           aes(x = state_density, y = weighted_density, color = highlight)) +<br>
  theme_bw() +<br>
  scale_x_log10(breaks = c(1, 3, 10, 30, 100, 300, 1000, 3000, 10000),<br>
                label = comma) +<br>
  scale_y_log10(breaks = c(1, 3, 10, 30, 100, 300, 1000, 3000, 10000),<br>
                label = comma) +<br>
  geom_point() +<br>
  geom_text_repel(aes(label = geographic_area)) +<br>
  geom_abline(slope = 1) +<br>
  theme(legend.position = &quot;none&quot;) +<br>
  labs(x = &quot;Unweighted Population Density&quot;, y = &quot;Weighted Population Density&quot;)<br>
p<br>
ggsave(<br>
  plot = p,<br>
  file = &quot;unweighted_v_weighted_density.png&quot;,<br>
  height = 8,<br>
  width = 8<br>
)<br>
#make a long version of result with two rows per state<br>
result_l = result %>%<br>
  mutate(sortval = weighted_density) %>%<br>
  gather(measure, density, state_density:weighted_density) %>%<br>
  arrange(sortval, measure) %>%<br>
  mutate(measure = factor(measure, levels = c(&quot;weighted_density&quot;, &quot;state_density&quot;)))<br>
#graph it<br>
p = ggplot(result_l, aes(<br>
  x = density,<br>
  # make the rows be states sorted by weighted density<br>
  y = reorder(geographic_area, sortval),<br>
  color = measure<br>
)) +<br>
  theme_bw() +<br>
  geom_point(size = 3) +<br>
  #connect the two measures for each state with a line<br>
  geom_line(aes(group = geographic_area), color = &quot;black&quot;) +<br>
  scale_x_log10(breaks = c(10, 30, 100, 300, 1000, 3000, 10000),<br>
                label = comma) +<br>
  theme(legend.position = &quot;bottom&quot;) +<br>
  labs(x = &quot;Population density&quot;, y = &quot;States ranked by weighted population density&quot;) +<br>
  scale_color_discrete(<br>
    name = &quot;&quot;,<br>
    breaks = c(&quot;weighted_density&quot;, &quot;state_density&quot;),<br>
    labels = c(&quot;Weighted Population Density&quot;, &quot;Unweighted Population Density&quot;)<br>
  )<br>
p<br>
ggsave(<br>
  plot = p,<br>
  file = &quot;state_v_unweighted_and_weighted_density.png&quot;,<br>
  height = 8,<br>
  width = 6<br>
)<br>
</code></p>
<p>H/T <a rel="nofollow" href="https://twitter.com/jakehofman" target="_blank">Jake Hofman</a> for getting me to do this and talking R.<br>
H/T to <a rel="nofollow" href="http://hadley.nz/" target="_blank">Hadley Wickham</a> for creating the <a rel="nofollow" href="http://tidyverse.org/" target="_blank">tidyverse</a>.</p>
<p>The post <a rel="nofollow" href="http://www.decisionsciencenews.com/2017/06/26/weighted-population-density/" target="_blank">Weighted population density</a> appeared first on <a rel="nofollow" href="http://www.decisionsciencenews.com/" target="_blank">Decision Science News</a>.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.decisionsciencenews.com/2017/06/26/weighted-population-density/"> R – Decision Science News</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/9e6iCDccXlw" height="1"></a></div>