<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/map-the-life-expectancy-in-united-states-with-data-from-wikipedia/"></div></div><div><a data-url="https://www.r-bloggers.com/map-the-life-expectancy-in-united-states-with-data-from-wikipedia/" data-counturl="https://www.r-bloggers.com/map-the-life-expectancy-in-united-states-with-data-from-wikipedia/" data-count="horizontal" data-text="Map the Life Expectancy in United States with data from Wikipedia" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://datascienceplus.com/map-the-life-expectancy-in-united-states-with-data-from-wikipedia/"> DataScience+</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Recently, I become interested to grasp the data from webpages, such as Wikipedia, and to visualize it with R. As I did in my <a target="_blank" href="http://datascienceplus.com/visualizing-obesity-across-united-states-by-using-data-from-wikipedia/">previous post</a>, I use <code>rvest</code> package to get the data from webpage and <code>ggplot</code> package to visualize the data.</p>
<p>In this post, I will map the life expectancy in White and African-American in US.</p>
<p>Load the required packages.</p>
<pre>
## LOAD THE PACKAGES ####
library(rvest)
library(ggplot2)
library(dplyr)
library(scales)</pre>
<p>Import the data from Wikipedia.</p>
<pre>
## LOAD THE DATA ####
le = read_html(&quot;https://en.wikipedia.org/wiki/List_of_U.S._states_by_life_expectancy&quot;)

le = le %>%
  html_nodes(&quot;table&quot;) %>%
  .[[2]]%>%
  html_table(fill=T)</pre>
<p>Now I have to clean the data. Below I have explain the role of each code.</p>
<pre>
## CLEAN THE DATA ####
# check the structure of dataset
str(le)<em>
'data.frame':	54 obs. of 417 variables:
$ X1 : chr &quot;&quot; &quot;Rank\nState\nLife Expectancy, All\n(in years)\nLife Expectancy, African American\n(in years)\nLife Expectancy, Asian American\n&quot;| __truncated__ &quot;Rank&quot; &quot;1&quot; ...
$ X2 : chr NA &quot;Rank&quot; &quot;State&quot; &quot;Hawaii&quot; ...
$ X3 : chr NA &quot;State&quot; &quot;Life Expectancy, All\n(in years)&quot; &quot;81.3&quot; ...
$ X4 : chr NA &quot;Life Expectancy, All\n(in years)&quot; &quot;Life Expectancy, African American\n(in years)&quot; &quot;-&quot; ...
$ X5 : chr NA &quot;Life Expectancy, African American\n(in years)&quot; &quot;Life Expectancy, Asian American\n(in years)&quot; &quot;82.0&quot; ...
$ X6 : chr NA &quot;Life Expectancy, Asian American\n(in years)&quot; &quot;Life Expectancy, Latino\n(in years)&quot; &quot;76.8&quot; ...
$ X7 : chr NA &quot;Life Expectancy, Latino\n(in years)&quot; &quot;Life Expectancy, Native American\n(in years)&quot; &quot;-&quot; ...
.....
.....</em>

# select only columns with data
le = le[c(1:8)]

# get the names from 3rd row and add to columns
names(le) = le[3,]

# delete rows and columns which I am not interested
le = le[-c(1:3), ]
le = le[, -c(5:7)]

# rename the names of 4th and 5th column
names(le)[c(4,5)] = c(&quot;le_black&quot;, &quot;le_white&quot;)

# make variables as numeric
le = le %>% 
  mutate(
    le_black = as.numeric(le_black), 
    le_white = as.numeric(le_white))

# check the structure of dataset
str(le)
<em>'data.frame':	51 obs. of 7 variables:
$ Rank : chr &quot;1&quot; &quot;2&quot; &quot;3&quot; &quot;4&quot; ...
$ State : chr &quot;Hawaii&quot; &quot;Minnesota&quot; &quot;Connecticut&quot; &quot;California&quot; ...
$ Life Expectancy, All
(in years): chr &quot;81.3&quot; &quot;81.1&quot; &quot;80.8&quot; &quot;80.8&quot; ...
$ le_black : num NA 79.7 77.8 75.1 78.8 77.4 NA NA 75.5 NA ...
$ le_white : num 80.4 81.2 81 79.8 80.4 80.5 80.4 80.1 80.3 80.1 ...
$ le_diff : num NA 1.5 3.2 4.7 1.6 ...
$ region : chr &quot;hawaii&quot; &quot;minnesota&quot; &quot;connecticut&quot; &quot;california&quot; ...</em>
</pre>
<p>Since there are some differences in life expectancy between White and African-American, I will calculate the differences and will map it.</p>
<pre>
le = le %>% mutate(le_diff = (le_white - le_black))</pre>
<p>I will load the map data and will merge the datasets togather.</p>
<pre>## LOAD THE MAP DATA ####
states = map_data(&quot;state&quot;)
str(states)
<em>'data.frame':	15537 obs. of 6 variables:
$ long : num -87.5 -87.5 -87.5 -87.5 -87.6 ...
$ lat : num 30.4 30.4 30.4 30.3 30.3 ...
$ group : num 1 1 1 1 1 1 1 1 1 1 ...
$ order : int 1 2 3 4 5 6 7 8 9 10 ...
$ region : chr &quot;alabama&quot; &quot;alabama&quot; &quot;alabama&quot; &quot;alabama&quot; ...
$ subregion: chr NA NA NA NA ...</em>

# create a new variable name for state
le$region = tolower(le$State)

# merge the datasets
states = merge(states, le, by=&quot;region&quot;, all.x=T)
str(states)
<em>'data.frame':	15537 obs. of 12 variables:
$ region : chr &quot;alabama&quot; &quot;alabama&quot; &quot;alabama&quot; &quot;alabama&quot; ...
$ long : num -87.5 -87.5 -87.5 -87.5 -87.6 ...
$ lat : num 30.4 30.4 30.4 30.3 30.3 ...
$ group : num 1 1 1 1 1 1 1 1 1 1 ...
$ order : int 1 2 3 4 5 6 7 8 9 10 ...
$ subregion : chr NA NA NA NA ...
$ Rank : chr &quot;49&quot; &quot;49&quot; &quot;49&quot; &quot;49&quot; ...
$ State : chr &quot;Alabama&quot; &quot;Alabama&quot; &quot;Alabama&quot; &quot;Alabama&quot; ...
$ Life Expectancy, All
(in years): chr &quot;75.4&quot; &quot;75.4&quot; &quot;75.4&quot; &quot;75.4&quot; ...
$ le_black : num 72.9 72.9 72.9 72.9 72.9 72.9 72.9 72.9 72.9 72.9 ...
$ le_white : num 76 76 76 76 76 76 76 76 76 76 ...
$ le_diff : num 3.1 3.1 3.1 3.1 3.1 ...</em></pre>
<p>Now its time to make the plot. First I will plot the life expectancy in African-American in US. For few states we don’t have the data, and therefore I will color it in grey color.</p>
<pre>## MAKE THE PLOT ####

# Life expectancy in African American
ggplot(states, aes(x = long, y = lat, group = group, fill = le_black)) + 
  geom_polygon(color = &quot;white&quot;) +
  scale_fill_gradient(name = &quot;Years&quot;, low = &quot;#ffe8ee&quot;, high = &quot;#c81f49&quot;, guide = &quot;colorbar&quot;, na.value=&quot;#eeeeee&quot;, breaks = pretty_breaks(n = 5)) +
  labs(title=&quot;Life expectancy in African American&quot;) +
  coord_map()
</pre>
<p>Here is the plot:<br>
<a title="" data-rel="lightbox-0" target="_blank" href="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2016/08/Le_african_american.png"><img sizes="(max-width: 490px) 100vw, 490px" srcset="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/08/Le_african_american-490x463.png?resize=456%2C431 490w, http://datascienceplus.com/wp-content/uploads/2016/08/Le_african_american-768x726.png 768w, http://datascienceplus.com/wp-content/uploads/2016/08/Le_african_american-600x567.png 600w, http://datascienceplus.com/wp-content/uploads/2016/08/Le_african_american-300x284.png 300w, http://datascienceplus.com/wp-content/uploads/2016/08/Le_african_american.png 800w" alt="Le_african_american" class="wp-image-4432" src="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/08/Le_african_american-490x463.png?resize=456%2C431" data-recalc-dims="1"></a></p>
<p>The code below is for White people in US.</p>
<pre>
# Life expectancy in White American
ggplot(states, aes(x = long, y = lat, group = group, fill = le_white)) + 
  geom_polygon(color = &quot;white&quot;) +
  scale_fill_gradient(name = &quot;Years&quot;, low = &quot;#ffe8ee&quot;, high = &quot;#c81f49&quot;, guide = &quot;colorbar&quot;, na.value=&quot;Gray&quot;, breaks = pretty_breaks(n = 5)) +
  labs(title=&quot;Life expectancy in White&quot;) +
  coord_map()</pre>
<p>Here is the plot:<br>
<a title="" data-rel="lightbox-1" target="_blank" href="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2016/08/Le_white.png"><img sizes="(max-width: 490px) 100vw, 490px" srcset="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/08/Le_white-490x463.png?resize=456%2C431 490w, http://datascienceplus.com/wp-content/uploads/2016/08/Le_white-768x726.png 768w, http://datascienceplus.com/wp-content/uploads/2016/08/Le_white-600x567.png 600w, http://datascienceplus.com/wp-content/uploads/2016/08/Le_white-300x284.png 300w, http://datascienceplus.com/wp-content/uploads/2016/08/Le_white.png 800w" alt="Le_white" class="wp-image-4430" src="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/08/Le_white-490x463.png?resize=456%2C431" data-recalc-dims="1"></a></p>
<p>Finally, I will map the differences between white and African American people in US.</p>
<pre>
# Differences in Life expectancy between White and African American
ggplot(states, aes(x = long, y = lat, group = group, fill = le_diff)) + 
  geom_polygon(color = &quot;white&quot;) +
  scale_fill_gradient(name = &quot;Years&quot;, low = &quot;#ffe8ee&quot;, high = &quot;#c81f49&quot;, guide = &quot;colorbar&quot;, na.value=&quot;#eeeeee&quot;, breaks = pretty_breaks(n = 5)) +
  labs(title=&quot;Differences in Life Expectancy between \nWhite and African Americans by States in US&quot;) +
  coord_map()</pre>
<p>Here is the plot:<br>
<a title="" data-rel="lightbox-2" target="_blank" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/08/Le_differences.png"><img sizes="(max-width: 490px) 100vw, 490px" srcset="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/08/Le_differences-490x463.png?resize=456%2C431 490w, http://datascienceplus.com/wp-content/uploads/2016/08/Le_differences-768x726.png 768w, http://datascienceplus.com/wp-content/uploads/2016/08/Le_differences-600x567.png 600w, http://datascienceplus.com/wp-content/uploads/2016/08/Le_differences-300x284.png 300w, http://datascienceplus.com/wp-content/uploads/2016/08/Le_differences.png 800w" alt="Le_differences" class="wp-image-4431" src="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/08/Le_differences-490x463.png?resize=456%2C431" data-recalc-dims="1"></a></p>
<p>On my <a target="_blank" href="http://datascienceplus.com/visualizing-obesity-across-united-states-by-using-data-from-wikipedia/">previous post</a> I got a comment to add the pop-up effect as I hover over the states. This is a simple task as <a target="_blank" href="http://datascienceplus.com/author/andreacirilloac/">Andrea</a> exmplained in his comment. What you have to do is to install the <code>plotly</code> package, to create a object for ggplot code above, like <code>map_data &lt;- ggplot(states, ... </code>, and then to use this function <code>ggplotly(map_plot)</code> to plot it.</p>
<p>Thats all! Leave a comment below if you have any question.</p>
<ul></ul>
<p><strong>
<p>Related Post</p>
<p>
<ol>
<li><a title="Permanent Link toWhat can we learn from the statistics of the EURO 2016 – Application of factor analysis" target="_blank" rel="bookmark" href="http://datascienceplus.com/what-can-we-learn-from-the-statistics-of-the-euro-2016-application-of-factor-analysis/">What can we learn from the statistics of the EURO 2016 – Application of factor analysis</a></li>
<li><a title="Permanent Link toVisualizing obesity across United States by using data from Wikipedia" target="_blank" rel="bookmark" href="http://datascienceplus.com/visualizing-obesity-across-united-states-by-using-data-from-wikipedia/">Visualizing obesity across United States by using data from Wikipedia</a></li>
<li><a title="Permanent Link toPlotting App for ggplot2 – Part 2" target="_blank" rel="bookmark" href="http://datascienceplus.com/plotting-app-for-ggplot2-part-2/">Plotting App for ggplot2 – Part 2</a></li>
<li><a title="Permanent Link toMastering R plot – Part 3: Outer margins" target="_blank" rel="bookmark" href="http://datascienceplus.com/mastering-r-plot-part-3-outer-margins/">Mastering R plot – Part 3: Outer margins</a></li>
<li><a title="Permanent Link toInteractive plotting with rbokeh" target="_blank" rel="bookmark" href="http://datascienceplus.com/interactive-plotting-with-rbokeh/">Interactive plotting with rbokeh</a></li>
</ol></strong>
<div>
<a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=wVAiBeGPsS0:ropjWvn46CU:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/datascienceplus?i=wVAiBeGPsS0:ropjWvn46CU:D7DqB2pKExk" border="0"></a> <a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=wVAiBeGPsS0:ropjWvn46CU:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/datascienceplus?d=qj6IDK7rITs" border="0"></a> <a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=wVAiBeGPsS0:ropjWvn46CU:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/datascienceplus?d=yIl2AUoC8zA" border="0"></a>
</div>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/map-the-life-expectancy-in-united-states-with-data-from-wikipedia/"></div></div><div><a data-url="https://www.r-bloggers.com/map-the-life-expectancy-in-united-states-with-data-from-wikipedia/" data-counturl="https://www.r-bloggers.com/map-the-life-expectancy-in-united-states-with-data-from-wikipedia/" data-count="vertical" data-text="Map the Life Expectancy in United States with data from Wikipedia" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://datascienceplus.com/map-the-life-expectancy-in-united-states-with-data-from-wikipedia/"> DataScience+</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/g2XylQDx1oc"></a></div>