<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/analyzing-obesity-across-usa/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://datascienceplus.com/analyzing-obesity-across-usa/"> R Programming – DataScience+</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>The main aim of this project is to study the states which had the most obese population among adults and children as well as teens in USA. Secondly, another objective of this project is to learn how to scrape data in R from an HTML page using <code>rvest</code> package and generate beautiful maps using <code>ggplot</code> and <code>maps</code> package in R. A <a rel="nofollow" href="https://datascienceplus.com/visualizing-obesity-across-united-states-by-using-data-from-wikipedia/" target="_blank">similar post</a> was published earlier at DataScience+.</p>
<h3>Loading the Required Packages</h3>
<pre>
require(rvest)
#rvest is the package to scrape Web pages in R
# ?rvest to search more on this package

require(ggplot2)
require(dplyr)
require(scales)
require(maps)</pre>
<p>Now Scraping Data from wikipedia article and converting it to a R Data frame</p>
<pre>
#Loading the Data

obesity&lt;-read_html(&quot;https://en.wikipedia.org/wiki/Obesity_in_the_United_States&quot;)
?read_html #for knowing more on this function

#html_nodes() to select a particular HTML element from the above page
#Converting to a R dataframe
#xpath of the Wikipedia table data
obesity = obesity %>%
  html_nodes(xpath='//*[@id=&quot;mw-content-text&quot;]/div/table[2]') %>% 
  .[[1]] %>%
  html_table(fill=T)


head(obesity)
<em>## State and District of Columbia Obese adults
## 1 Alabama 30.1%
## 2 Alaska 27.3%
## 3 Arizona 23.3%
## 4 Arkansas 28.1%
## 5 California 23.1%
## 6 Colorado 21.0%
## Overweight (incl. obese) adults Obese children and adolescents
## 1 65.4% 16.7%
## 2 64.5% 11.1%
## 3 59.5% 12.2%
## 4 64.7% 16.4%
## 5 59.4% 13.2%
## 6 55.0% 9.9%
## Obesity rank
## 1 3
## 2 14
## 3 40
## 4 9
## 5 41
## 6 51
</em>
</pre>
<p>Data Transformation</p>
<pre>
#to check the structure of 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>
</pre>
<p>We need to remove the ‘%’ from the data and convert it to numeric data type to draw plots using <code>ggplot</code> package.</p>
<pre>
#removing the % and making the data numeric

for(i in 2:4){
  obesity[,i] = gsub(&quot;%&quot;, &quot;&quot;, obesity[,i])
  obesity[,i] = as.numeric(obesity[,i])
}

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 fixing the attributes Names to remove spaces between them.</p>
<pre>
#Fixing the names to remove spaces

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>We will use <code>make.names</code> to remove spaces and make syntactically valid names.</p>
<pre>
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>
<h3>Loading the Map data for USA and merging it with Obesity Data frame</h3>
<p><code>maps</code> package in R provides methods to load the geographical data of different countries and the world in R to a data frame consisting of Latitudes and Longitudes as well which can further be used to generate and visualize maps in R.</p>
<pre>
#Loading the map data-----------------

states = map_data(&quot;state&quot;)
?map_data to read more on using this function

# create a new variable region for state
obesity$region = tolower(obesity$State.and.District.of.Columbia)

#merging the datasets
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>Visualizing the Data</h2>
<pre>
#a data frame for adding Names to the states on the Map- making a new data frame

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>Now finding the top 10 states with most Obese Adult population using <code>dplyr</code> package.</p>
<pre>
#Data frame consisting of top 10 Most Obese Adults States 
topstate = states %>% 
  group_by(region) %>%
  summarise(
    
    Obese.adults = mean(Obese.adults)
   )
   %>%
  arrange(desc(Obese.adults)) %>%
  top_n(10)
</pre>
<h3>Plotting the States with Most Obese Adults</h3>
<p>Making a Barplot.</p>
<pre>
#Plotting the top 10 states 

ggplot(aes(x = reorder(region,Obese.adults), y = Obese.adults),data = topstate) + 
  geom_col(color=&quot;black&quot;,fill=&quot;#1EDBC2&quot;,alpha=0.6) +
  labs(y = &quot;Percentage of Obese Adults&quot;,x=&quot;Top 10 States&quot;) +
  coord_flip()

</pre>
<p>Gives this plot:<br>
<a rel="nofollow" href="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2017/07/download.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2017/07/download-490x392.png?w=450&ssl=1" alt="" class="wp-image-7522" data-recalc-dims="1"></a></p>
<p>From the Barplot we notice that the State with Highest Obese Adult Population is Mississippi.</p>
<h3>Plotting a Map for Most Obese Adult Population</h3>
<pre>
#Plotting the data on a map------------------------


#For obese adults

ggplot(states, aes(x = long, y = lat, group = group, fill = Obese.adults)) + 
  geom_polygon(color = &quot;white&quot;,show.legend = T) +
  scale_fill_gradient(name = &quot;Percent&quot;, low = &quot;#FAB8D2&quot;, high = &quot;#F91C74&quot;, guide = &quot;colorbar&quot;, na.value=&quot;black&quot;, breaks = pretty_breaks(n = 5)) +
  labs(title=&quot;Obesity in Adults for USA&quot;,x = &quot;Longitude&quot;,y = &quot;Latitude&quot;) +
  coord_map() +
  #adding States names to the states on the map
  geom_text(data=statenames, aes(x = long, y = lat, label = region), size=3)
</pre>
<p>Gives this plot:<br>
<a rel="nofollow" href="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2017/07/download-1.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2017/07/download-1-490x392.png?w=450&ssl=1" alt="" class="wp-image-7526" data-recalc-dims="1"></a></p>
<p>The darker regions on the map indicate the State with highest percentage of Obese Adult population. The state with minimum obese adult population is Connecticut.</p>
<h3>Obesity in Children and Teen Population</h3>
<p>Creating an new data frame with Top 15 States with Most Obese Children and Teens Population.</p>
<pre>
#Now Analyzing the Obese Children and Teens

#Finding top 15 States with Most Obese Children and Teens
topChild = states %>%
      group_by(region) %>%
      summarise(Obese.Child.and.Teens = mean(Obese.children.and.adolescents)) %>%
      top_n(15)
</pre>
<p>Making a Barplot</p>
<pre>
#Barplot

ggplot(data = topChild, aes(x = reorder(region,Obese.Child.and.Teens), y = Obese.Child.and.Teens))+
        geom_col(color=&quot;black&quot;,fill=&quot;#6EE543&quot;,alpha=0.8) +
        coord_flip()
</pre>
<p>Gives this plot:<br>
<a rel="nofollow" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2017/07/download-2.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2017/07/download-2-490x392.png?w=450&ssl=1" alt="" class="wp-image-7530" data-recalc-dims="1"></a></p>
<p>As we can notice the state with most Obese children and Teens is Delaware.</p>
<pre>
#Map for Obesity in Children 

ggplot(states, aes(x = long, y = lat, group = group, fill = Obese.children.and.adolescents)) + 
  geom_polygon(color = &quot;white&quot;) +
  scale_fill_gradient(name = &quot;Percent Obese&quot;, low = &quot;#B8D5EC&quot;, high = &quot;#0A4B7D&quot;, guide = &quot;colorbar&quot;, na.value=&quot;black&quot;, breaks = pretty_breaks(n = 5)) +
  labs(title=&quot;Obesity in Children and Teens&quot;, x = &quot;Longitude&quot;,y = &quot;latitude&quot;) +
  coord_map() + 
  #adding States names to the states on the map
  geom_text(data=statenames, aes(x = long, y = lat, label = region), size=3)
</pre>
<p>Gives this plot:<br>
<a rel="nofollow" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2017/07/download-3.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2017/07/download-3-490x392.png?w=450&ssl=1" alt="" class="wp-image-7535" data-recalc-dims="1"></a></p>
<p>Now let’s plot a complete Barplot of States and Percentage of Obese Children and Teens</p>
<pre>
ggplot(aes(x = reorder(region,Obese.children.and.adolescents),y = Obese.children.and.adolescents),
           data = statenames) + 
  geom_col(color=&quot;black&quot;,fill=&quot;#F43E3E&quot;,width=1)   +
  coord_flip() + 
  labs(x = &quot;States&quot;, y =&quot;Percentage of Obese Children and Teens&quot;,title=&quot;Barplot of Obese Children and Teens&quot;)
</pre>
<p>Gives this plot:<br>
<a rel="nofollow" href="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2017/07/download-4.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2017/07/download-4-490x392.png?w=450&ssl=1" alt="" class="wp-image-7536" data-recalc-dims="1"></a></p>
<p>The State with Highest Obese Teen and Children population is Delaware and the state with least obese teens and children is Utah.</p>
<h2>Conclusion</h2>
<p>In this project we firstly learned to scrape data using <code>rvest</code> package from wikipedia and then analyzed and visualized the States with most Obese Adult and children population.We also learned how to create beautifull maps using <code>ggplot</code> and <code>maps</code> packages in R.</p>
<p>Hope you guys liked the article and is interesting enough to get you started with scraping data from any web document and start analyzing it yourself in R and create beautiful maps and plots.</p>
<p>Make sure to like and share it.Cheers ! </p>
<ul></ul>
<p><strong>
<p>Related Post</p>
<p>
<ol>
<li><a rel="nofollow" href="https://datascienceplus.com/can-we-predict-flu-deaths-with-machine-learning-and-r/" title="Permanent Link toCan we predict flu deaths with Machine Learning and R?" target="_blank">Can we predict flu deaths with Machine Learning and R?</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/graphical-presentation-of-missing-data-vim-package/" title="Permanent Link toGraphical Presentation of Missing Data; VIM Package" target="_blank">Graphical Presentation of Missing Data; VIM Package</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/creating-graphs-with-python-and-goopycharts/" title="Permanent Link toCreating Graphs with Python and GooPyCharts" target="_blank">Creating Graphs with Python and GooPyCharts</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/visualizing-tennis-grand-slam-winners-performances/" title="Permanent Link toVisualizing Tennis Grand Slam Winners Performances" target="_blank">Visualizing Tennis Grand Slam Winners Performances</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/plotting-data-online-via-plotly-and-python/" title="Permanent Link toPlotting Data Online via Plotly and Python" target="_blank">Plotting Data Online via Plotly and Python</a></li>
</ol></strong>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://datascienceplus.com/analyzing-obesity-across-usa/"> R Programming – 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 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/0ocraoQs5Ps" height="1"></a></div>