<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/visualizing-obesity-across-united-states-by-using-data-from-wikipedia/"></div></div><div><a data-url="http://www.r-bloggers.com/visualizing-obesity-across-united-states-by-using-data-from-wikipedia/" data-counturl="http://www.r-bloggers.com/visualizing-obesity-across-united-states-by-using-data-from-wikipedia/" data-count="horizontal" data-text="Visualizing obesity across United States by using 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/visualizing-obesity-across-united-states-by-using-data-from-wikipedia/"> DataScience+</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In this post I will show how to collect from a webpage and to analyze or visualize in R. For this task I will use the <code>rvest</code> package and will get the data from Wikipedia. I got the idea to write this post from <a target="_blank" href="http://datascienceplus.com/author/fisseha-berhane/">Fisseha Berhane</a>. </p>
<p>I will gain access to the prevalence of obesity in United States from <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Obesity_in_the_United_States">Wikipedia page</a>, then I will plot it in the map. Lets begin with loading the required packages.</p>
<pre>## LOAD THE PACKAGES ####
library(rvest)
library(ggplot2)
library(dplyr)
library(scales)</pre>
<p>After I loaded the packages in R, will upload the data. As I mention before, I will download the data from Wikipedia. </p>
<pre>## LOAD THE DATA ####
obesity = read_html(&quot;https://en.wikipedia.org/wiki/Obesity_in_the_United_States&quot;)

obesity = obesity %>%
     html_nodes(&quot;table&quot;) %>%
     .[[1]]%>%
     html_table(fill=T)</pre>
<p>The first line of code is calling the data from Wikipedia and the second line of codes is transforming the table that we are interested into dataframe in R. </p>
<p>Now lets check how our date looks alike.</p>
<pre>head(obesity)<em>
State and District of Columbia Obese adults Overweight (incl. obese) adults
1 Alabama 30.1% 65.4%
2 Alaska 27.3% 64.5%
3 Arizona 23.3% 59.5%
4 Arkansas 28.1% 64.7%
5 California 23.1% 59.4%
6 Colorado 21.0% 55.0%
Obese children and adolescents Obesity rank
1 16.7% 3
2 11.1% 14
3 12.2% 40
4 16.4% 9
5 13.2% 41
6 9.9% 51</em></pre>
<p>The dataframe looks good, now we need to clean it from making ready to plot.</p>
<pre>## CLEAN THE DATA ####
str(obesity)<em>
'data.frame':	51 obs. of 5 variables:
$ State and District of Columbia : chr &quot;Alabama&quot; &quot;Alaska&quot; &quot;Arizona&quot; &quot;Arkansas&quot; ...
$ Obese adults : chr &quot;30.1%&quot; &quot;27.3%&quot; &quot;23.3%&quot; &quot;28.1%&quot; ...
$ Overweight (incl. obese) adults: chr &quot;65.4%&quot; &quot;64.5%&quot; &quot;59.5%&quot; &quot;64.7%&quot; ...
$ Obese children and adolescents : chr &quot;16.7%&quot; &quot;11.1%&quot; &quot;12.2%&quot; &quot;16.4%&quot; ...
$ Obesity rank : int 3 14 40 9 41 51 49 43 22 39 ...</em>

# remove the % and make the data numeric
for(i in 2:4){
     obesity[,i] = gsub(&quot;%&quot;, &quot;&quot;, obesity[,i])
     obesity[,i] = as.numeric(obesity[,i])
}

# check data again
str(obesity)<em>
'data.frame':	51 obs. of 5 variables:
$ State and District of Columbia : chr &quot;Alabama&quot; &quot;Alaska&quot; &quot;Arizona&quot; &quot;Arkansas&quot; ...
$ Obese adults : num 30.1 27.3 23.3 28.1 23.1 21 20.8 22.1 25.9 23.3 ...
$ Overweight (incl. obese) adults: num 65.4 64.5 59.5 64.7 59.4 55 58.7 55 63.9 60.8 ...
$ Obese children and adolescents : num 16.7 11.1 12.2 16.4 13.2 9.9 12.3 14.8 22.8 14.4 ...
$ Obesity rank : int 3 14 40 9 41 51 49 43 22 39 ...</em></pre>
<p>Now we will fix the names of variables by removing the spaces</p>
<pre>names(obesity)<em>
[1] &quot;State and District of Columbia&quot; &quot;Obese adults&quot; [3] &quot;Overweight (incl. obese) adults&quot; &quot;Obese children and adolescents&quot; [5] &quot;Obesity rank&quot;</em>

names(obesity) = make.names(names(obesity))
names(obesity)<em>
[1] &quot;State.and.District.of.Columbia&quot; &quot;Obese.adults&quot; [3] &quot;Overweight..incl..obese..adults&quot; &quot;Obese.children.and.adolescents&quot; [5] &quot;Obesity.rank&quot;</em></pre>
<p>Our data looks good. Its time to load the map data</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></pre>
<p>We will merge two datasets (<em>obesity</em> and <em>states</em>) by region, therefore we need first to create new variable (region) in <em>obesity</em> dataset. </p>
<pre># create a new variable name for state
obesity$region = tolower(obesity$State.and.District.of.Columbia)</pre>
<p>Now we will merge the datasets.</p>
<pre>states = merge(states, obesity, by=&quot;region&quot;, all.x=T)
str(states)<em>
'data.frame':	15537 obs. of 11 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 ...
$ State.and.District.of.Columbia : chr &quot;Alabama&quot; &quot;Alabama&quot; &quot;Alabama&quot; &quot;Alabama&quot; ...
$ Obese.adults : num 30.1 30.1 30.1 30.1 30.1 30.1 30.1 30.1 30.1 30.1 ...
$ Overweight..incl..obese..adults: num 65.4 65.4 65.4 65.4 65.4 65.4 65.4 65.4 65.4 65.4 ...
$ Obese.children.and.adolescents : num 16.7 16.7 16.7 16.7 16.7 16.7 16.7 16.7 16.7 16.7 ...
$ Obesity.rank : int 3 3 3 3 3 3 3 3 3 3 ...</em></pre>
<h2>Plot the data</h2>
<p>Finally we will plot the prevalence of obesity in adults.</p>
<pre>## MAKE THE PLOT ####

# adults
ggplot(states, aes(x = long, y = lat, group = group, fill = Obese.adults)) + 
     geom_polygon(color = &quot;white&quot;) +
     scale_fill_gradient(name = &quot;Percent&quot;, low = &quot;#feceda&quot;, high = &quot;#c81f49&quot;, guide = &quot;colorbar&quot;, na.value=&quot;black&quot;, breaks = pretty_breaks(n = 5)) +
     labs(title=&quot;Prevalence of Obesity in Adults&quot;) +
     coord_map()</pre>
<p>Here is the plot in adults:<br>
<a title="" data-rel="lightbox-0" target="_blank" href="http://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/06/adults.png"><img sizes="(max-width: 490px) 100vw, 490px" srcset="http://i0.wp.com/datascienceplus.com/wp-content/uploads/2016/06/adults-490x244.png?resize=456%2C227 490w, http://datascienceplus.com/wp-content/uploads/2016/06/adults-768x383.png 768w, http://datascienceplus.com/wp-content/uploads/2016/06/adults-600x299.png 600w, http://datascienceplus.com/wp-content/uploads/2016/06/adults-300x150.png 300w, http://datascienceplus.com/wp-content/uploads/2016/06/adults.png 800w" alt="adults" class="wp-image-4075" src="http://i0.wp.com/datascienceplus.com/wp-content/uploads/2016/06/adults-490x244.png?resize=456%2C227" data-recalc-dims="1"></a></p>
<p>Similarly, we can plot the prevalence of obesity in children.</p>
<pre># children
ggplot(states, aes(x = long, y = lat, group = group, fill = Obese.adults)) + 
     geom_polygon(color = &quot;white&quot;) +
     scale_fill_gradient(name = &quot;Percent&quot;, low = &quot;#feceda&quot;, high = &quot;#c81f49&quot;, guide = &quot;colorbar&quot;, na.value=&quot;black&quot;, breaks = pretty_breaks(n = 5)) +
     labs(title=&quot;Prevalence of Obesity in Adults&quot;) +
     coord_map()</pre>
<p>Here is the plot in children:<br>
<a title="" data-rel="lightbox-1" target="_blank" href="http://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/06/children.png"><img sizes="(max-width: 490px) 100vw, 490px" srcset="http://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/06/children-490x244.png?resize=456%2C227 490w, http://datascienceplus.com/wp-content/uploads/2016/06/children-768x383.png 768w, http://datascienceplus.com/wp-content/uploads/2016/06/children-600x299.png 600w, http://datascienceplus.com/wp-content/uploads/2016/06/children-300x150.png 300w, http://datascienceplus.com/wp-content/uploads/2016/06/children.png 800w" alt="children" class="wp-image-4076" src="http://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/06/children-490x244.png?resize=456%2C227" data-recalc-dims="1"></a></p>
<p>If you like to show the name of State in the map use the code below to create a new dataset.</p>
<pre>statenames = states %>% 
     group_by(region) %>%
     summarise(
          long = mean(range(long)), 
          lat = mean(range(lat)), 
          group = mean(group), 
          Obese.adults = mean(Obese.adults), 
          Obese.children.and.adolescents = mean(Obese.children.and.adolescents)
 )</pre>
<p>After you add this code to ggplot code above</p>
<pre>geom_text(data=statenames, aes(x = long, y = lat, label = region), size=3)</pre>
<p>That’s all. I hope you learned something useful today.</p>
<ul></ul>
<p><strong>
<p>Related Post</p>
<p>
<ol>
<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>
<li><a title="Permanent Link toMastering R plot – Part 2: Axis" target="_blank" rel="bookmark" href="http://datascienceplus.com/mastering-r-plot-part-2-axis/">Mastering R plot – Part 2: Axis</a></li>
<li><a title="Permanent Link toHow to create a Twitter Sentiment Analysis using R and Shiny" target="_blank" rel="bookmark" href="http://datascienceplus.com/how-to-create-a-twitter-sentiment-analysis-using-r-and-shiny/">How to create a Twitter Sentiment Analysis using R and Shiny</a></li>
</ol></strong>
<div>
<a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=OS6Rbx4M07U:n6D-t_DJi-g:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/datascienceplus?i=OS6Rbx4M07U:n6D-t_DJi-g:D7DqB2pKExk" border="0"></a> <a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=OS6Rbx4M07U:n6D-t_DJi-g: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=OS6Rbx4M07U:n6D-t_DJi-g: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="http://www.r-bloggers.com/visualizing-obesity-across-united-states-by-using-data-from-wikipedia/"></div></div><div><a data-url="http://www.r-bloggers.com/visualizing-obesity-across-united-states-by-using-data-from-wikipedia/" data-counturl="http://www.r-bloggers.com/visualizing-obesity-across-united-states-by-using-data-from-wikipedia/" data-count="vertical" data-text="Visualizing obesity across United States by using 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/visualizing-obesity-across-united-states-by-using-data-from-wikipedia/"> DataScience+</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://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="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/iqUDQu8KsDA"></a></div>