<p><a rel="nofollow" href="http://www.sharpsightlabs.com/join-newsletter-get-crash-course/" target="_blank"><br>
<img sizes="(max-width: 1612px) 100vw, 1612px" src="https://i1.wp.com/www.sharpsightlabs.com/wp-content/uploads/2018/03/NYC-best-us-metro-gdp.png?w=450" alt="" class="wp-image-3397" data-recalc-dims="1"><br>
</a></p>
<p>It’s clear that there are some economic shifts happening in the world, if not the US itself.</p>
<p>In light of this, I decided to do some simple investigation into the economic performance of US cities.</p>
<p>This is, by the way, one of the critical reasons to <a rel="nofollow" href="http://www.sharpsightlabs.com/blog/master-r-obsolete/" target="_blank">master data science</a>. One you know a few critical skills, you will be able to very rapidly get some basic information about (almost) any topic. </p>
<p>In a case such as this (when you’re just personally interested), you can just scrape some data and plot it.</p>
<p>But if you’re working in a business, you will <em>need</em> to be able to <a rel="nofollow" href="http://www.sharpsightlabs.com/blog/data-visualizations-are-tools-and-what-youre-building/" target="_blank">generate these types of insights quickly</a>. A large part of your job will be gathering data and quickly plotting it in ways that generate insight … </p>
<h1>Plotting GDP data for top US cities</h1>
<p>In the following code, we’ll scrape some data about US cities and <a rel="nofollow" href="http://www.sharpsightlabs.com/blog/line-chart-ggplot2-amzn/" target="_blank">plot a line chart using <code>ggplot2</code></a>.</p>
<p>There’s actually quite a bit more that we could do with this data, so feel free to create your own plots and leave the code in the comments below.</p>
<pre class="wp-code-highlight">
#=================
# INSTALL PACKAGES
#=================
library(tidyverse)
library(stringr)
library(forcats)
library(rvest)
library(ggthemes)


#============
# SCRAPE DATA
#============
df.metro_gdp &lt;- read_html('https://en.wikipedia.org/wiki/List_of_U.S._metropolitan_areas_by_GDP') %>% 
  html_nodes('table') %>% 
  .[[1]] %>% 
  html_table() %>% 
  as.tibble()


#=======================
# REMOVE 'Rank' VARIABLE
#=======================
df.metro_gdp &lt;- df.metro_gdp %>% 
  select(-Rank)


#================
# RENAME VARIABLE
#================
df.metro_gdp &lt;- df.metro_gdp %>% rename(metro_area = `Metropolitan area`)


# inspect
df.metro_gdp


# REMOVE 'MSA' FROM metro_area
df.metro_gdp &lt;- df.metro_gdp %>% mutate(metro_area = str_replace(metro_area, ' MSA', ''))


# COERCE TO 'metro_area' FACTOR
df.metro_gdp &lt;- df.metro_gdp %>% mutate(metro_area = metro_area %>% as_factor())


#========================================================
# CREATE NEW VARIABLE: 
# - the original 'metro_area' variable is rather long
#   because it's  a full 'metropolitan statistical area'
# - we can abbreviate these as the plain city name
# - we'll call the new variable 'metro_brief'
#========================================================

# get unique values
df.metro_gdp %>% 
  select(metro_area) %>% 
  unique()


#---------------------------------------------------
# RECODE VALUES
# here we will create the new variable 'metro_brief'
#---------------------------------------------------
df.metro_gdp &lt;- df.metro_gdp %>% 
  mutate(metro_area_brief = recode(metro_area,'New York–Northern New Jersey–Long Island, NY–NJ–PA' = 'New York'
         ,'Los Angeles–Long Beach–Santa Ana, CA' = 'Los Angeles'
         ,'Chicago–Joliet–Naperville, IL–IN–WI' = 'Chicago'
         ,'Dallas–Fort Worth–Arlington, TX' = 'Dallas'
         ,'Washington–Arlington–Alexandria, DC–VA–MD–WV' = 'Washington DC'
         ,'Houston–Sugar Land–Baytown, TX' = 'Houston'
         ,'San Francisco–Oakland–Fremont, CA' = 'San Francisco'
         ,'Philadelphia–Camden–Wilmington, PA–NJ–DE–MD' = 'Philadelphia'
         ,'Boston–Cambridge–Quincy, MA–NH' = 'Boston'
         ,'Atlanta–Sandy Springs–Marietta, GA' = 'Atlanta'
         ))



# INSPECT VALUES
df.metro_gdp %>% glimpse()
df.metro_gdp %>% select(metro_area_brief)


# CHECK TABLE OF CROSS-VALUES
df.metro_gdp %>% 
  #select(metro_area, metro_brief) %>% 
  group_by(metro_area, metro_area_brief) %>% 
  summarise()


#======================
# RESHAPE: WIDE TO LONG
#======================
df.metro_gdp &lt;- df.metro_gdp %>% gather(key = year, value = gdp_nominal, -metro_area, -metro_area_brief)


#========================
# COERCE 'year' TO FACTOR
#========================
df.metro_gdp &lt;- df.metro_gdp %>% mutate(year = year %>% as.factor())


#===========================================
# WRANGLE AND COERCE 'gdp_nominal' TO DOUBLE
#===========================================
df.metro_gdp &lt;- mutate(df.metro_gdp, gdp_nominal = str_remove_all(gdp_nominal, &quot;,&quot;) %>% as.double())


#================
# PLOT BASIC PLOT
#================
ggplot(df.metro_gdp, aes(x = year, y = gdp_nominal, group = metro_area_brief)) +
  geom_line(aes(color = metro_area_brief))



#==========
# FORMATTED
#==========


df.metro_gdp %>% 
  mutate(highlight_flag = if_else(metro_area_brief == 'New York', T, F)) %>%
  ggplot(aes(x = year, y = gdp_nominal, group = metro_area_brief)) +
    geom_line(aes(color = highlight_flag, alpha = highlight_flag), size = 1.5) +
    scale_color_manual(values = c('grey', 'red')) +
    scale_alpha_manual(values = c(.7, 1)) +
    labs(title = 'New York is the best performing US city by metro GDP'
         ,subtitle = str_c(&quot;Consistently, New York has a much higher GDP than other metro areas.&quot;
                           ,&quot;\n77% higher than next highest metro in 2017.&quot;)
         ,y = &quot;Nominal GDP\n(metro area, millions of dollars)&quot;
         ,x = 'Year') +
    theme(legend.position = 'none'
          ,text = element_text(color = '#3A3A3A'
                               ,family = 'sans')
          ,plot.title = element_text(margin = margin(b = 10)
                                     ,face = 'bold'
                                     ,size = 20)
          ,axis.title = element_text()
          ,plot.subtitle = element_text(size = 12)
          ) +
     scale_y_continuous(labels = scales::comma_format())


</pre>
<p>And here is the finalized chart:</p>
<p><a rel="nofollow" href="http://www.sharpsightlabs.com/join-newsletter-get-crash-course/" target="_blank"><br>
<img sizes="(max-width: 1612px) 100vw, 1612px" src="https://i1.wp.com/www.sharpsightlabs.com/wp-content/uploads/2018/03/NYC-best-us-metro-gdp.png?w=450" alt="" class="wp-image-3397" data-recalc-dims="1"><br>
</a></p>
<h1>Sign up now, and get access to our free Data Science Crash Course</h1>
<p>Want to learn more about data analysis and data science?</p>
<p>Sign up now for our email list, and you’ll get access to our <strong>free</strong> Data Science Crash Course.</p>
<p>In the Data Science Crash Course, you’ll learn:</p>
<ul>
<li>a step-by-step data science learning plan </li>
<li>the 1 programming language you <i>need to</i> learn </li>
<li>3 essential data visualizations</li>
<li>how to do data manipulation in R</li>
<li>how to get started with machine learning</li>
<li>the difference between machine learning and statistics</li>
</ul>
<p><a rel="nofollow" href="http://www.sharpsightlabs.com/join-newsletter-get-crash-course/" target="_blank">
<p><strong>SIGN UP NOW</strong></p>
<p></p></a>
<p>The post <a rel="nofollow" href="http://www.sharpsightlabs.com/blog/us-metro-gdp/" target="_blank">Plotting US Metro Area GDP with ggplot</a> appeared first on <a rel="nofollow" href="http://www.sharpsightlabs.com/" target="_blank">SHARP SIGHT</a>.</p>
	<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/k6tfbIDG2ZM" height="1">