<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/mapping-world-cities-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://sharpsightlabs.com/blog/mapping-world-cities/"> r-bloggers – SHARP SIGHT LABS</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a href="http://sharpsightlabs.com/join-newsletter-rapid-mastery/"><br>
<img sizes="(max-width: 600px) 100vw, 600px" src="https://i1.wp.com/sharpsightlabs.com/wp-content/uploads/2017/11/alpha_cities_faceted_2-600x492.png?w=450" alt="" class="wp-image-3166" data-recalc-dims="1"><br>
</a></p>
<p>Here at <a href="http://sharpsightlabs.com/">Sharp Sight</a>, we make a lot of maps.</p>
<p>There are a few reasons for this.</p>
<p>First, good maps are typically ‘information dense.’ You can get a lot of information at a glance from a good map. They are good visualization tools for <a href="http://sharpsightlabs.com/blog/data-visualizations-are-tools-and-what-youre-building/">finding and communicating insights</a>.</p>
<p>Second, it’s extremely easy to get data that you can use to make a map. From a variety of sources, you’ll find data about <a href="http://sharpsightlabs.com/blog/new-amazon-hq/">cities</a>, states, <a href="http://sharpsightlabs.com/blog/map-unemployment-nov-2016/">counties</a>, and <a href="http://sharpsightlabs.com/blog/human-capital-map/">countries</a>. If you know how to retrieve this data and <a href="https://www.r-bloggers.com/mapping-world-cities-in-r/sharpsightlabs.com/blog/dplyr-intro-data-manipulation-with-r/">wrangle it into shape</a>, it will be easy to find data that you can use to make a map.</p>
<p>Finally, map making is just good practice. To create a map like the one we’re about to make, you’ll typically need to use a variety of data wrangling and data visualization tools. Maps make for excellent practice for intermediate data scientists who have already mastered some of the basics.</p>
<p>With that in mind, this week we’ll make a map of “<a href="https://en.wikipedia.org/wiki/Global_city">world cities</a>.” This set of cities has been identified by the Globalization and World Cities (GaWC) Research Network as being highly connected and influential in the world economy.</p>
<p>We’re going to initially create a very basic map, but we’ll also create a small multiple version of the map (broken out by GaWC ranking).</p>
<p>Let’s get started.</p>
<p>First, we’ll load the packages that we’ll need.</p>
<pre class="wp-code-highlight">

#==============
# LOAD PACKAGES
#==============

library(tidyverse)
library(ggmap)
library(forcats)

</pre>
<p>Next, we’ll input the cities by hard coding them as data frames. To be clear, there is more than one way to do this (e.g., we could scrape the data), but there isn’t that much data here, so doing this manually is acceptable.</p>
<pre class="wp-code-highlight">
#===================
# INPUT ALPHA CITIES
#===================

df_alpha_plus_plus &lt;- tibble(city = c('London','New York'))
df_alpha_plus &lt;- tibble(city = c('Singapore', 'Hong Kong', 'Paris', 'Beijing'
                                 ,'Tokyo', 'Dubai', 'Shanghai'))
df_alpha &lt;- tibble(city = c('Sydney', 'São Paulo', 'Milan', 'Chicago'
                            ,'Mexico City', 'Mumbai', 'Moscow', 'Frankfurt'
                            ,'Madrid', 'Warsaw', 'Johannesburg', 'Toronto'
                            ,'Seoul', 'Istanbul', 'Kuala Lumpur', 'Jakarta'
                            ,'Amsterdam', 'Brussels', 'Los Angeles'))
df_alpha_minus &lt;- tibble(city = c('Dublin', 'Melbourne', 'Washington', 'New Delhi'
                                  ,'Bangkok', 'Zurich', 'Vienna', 'Taipei'
                                  ,'Buenos Aires', 'Stockholm', 'San Francisco'
                                  ,'Guangzhou', 'Manila', 'Bogotá', 'Miami', 'Luxembourg'
                                  ,'Riyadh', 'Santiago', 'Barcelona', 'Tel Aviv', 'Lisbon'))

</pre>
<p>Now, we’ll create a new variable called . This will contain the global city rating.</p>
<p>Notice that this is a <a href="https://www.r-bloggers.com/mapping-world-cities-in-r/sharpsightlabs.com/blog/dplyr-intro-data-manipulation-with-r/">very straightforward use of </a>, one of the tidyverse functions you should definitely master.</p>
<pre class="wp-code-highlight">
#=======================
# ADD GLOBAL CITY RATING
#=======================

df_alpha_plus_plus &lt;- df_alpha_plus_plus %>% mutate(rating = 'alpha++')
df_alpha_plus &lt;- df_alpha_plus %>% mutate(rating = 'alpha+')
df_alpha &lt;- df_alpha %>% mutate(rating = 'alpha')
df_alpha_minus &lt;- df_alpha_minus %>% mutate(rating = 'alpha-')

</pre>
<p>Next, we’ll combine the different data frames into one using .</p>
<pre class="wp-code-highlight">
#======================================
# COMBINE DATAFRAMES INTO ONE DATAFRAME
#======================================

alpha_cities &lt;- rbind(df_alpha_plus_plus
      ,df_alpha_plus
      ,df_alpha
      ,df_alpha_minus
      )

</pre>
<p>Now that the data are combined into a single data frame, we’ll get the longitude and latitude using .</p>
<pre class="wp-code-highlight">
#========
# GEOCODE
#========

latlong &lt;- geocode(alpha_cities$city)



#========
# INSPECT
#========

alpha_cities
latlong

</pre>
<p>Once we have the longitude and latitude data, we need to combine it with the original data in the  data frame. To do this, we will use .</p>
<pre class="wp-code-highlight">
#============================
# BIND LAT/LONG TO CITY NAMES
#============================

alpha_cities &lt;- cbind(alpha_cities, latlong) %>% rename(long = lon)

alpha_cities

#names(alpha_cities)

</pre>
<p>Now we have the data that we need, but we’ll need to clean things up a little.</p>
<p>In the visualization we’ll make, we will need to use the faceting technique from . When we do this, we’ll facet on the  variable, but we will need the levels of that variable to be ordered properly (otherwise the facets will be out of order). </p>
<p>To reorder the factor levels of , we will use .</p>
<pre class="wp-code-highlight">
#================================================
# REORDER LEVELS OF GLOBAL CITY RATING 
# - the global city ratings should be ordered
#   i.e., alpha++, then alpha+ ....
# - to do this, we'll use forecats::fct_relevel()
#================================================

alpha_cities &lt;- mutate(alpha_cities, rating = fct_relevel(rating, 'alpha++','alpha+','alpha','alpha-'))

levels(alpha_cities$rating)

</pre>
<p>Because we will be building a map, we’ll need to retrive a map of the world. We can get a world map by using .</p>
<pre class="wp-code-highlight">
#==============
# GET WORLD MAP
#==============

map_world &lt;- map_data(&quot;world&quot;)

</pre>
<p>Ok. We basically have everything we need. Now we will make a simple first draft.</p>
<pre class="wp-code-highlight">
#================
# FIRST DRAFT MAP
#================

ggplot() +
  geom_polygon(data = map_world, aes(x = long, y = lat, group = group)) +
  geom_point(data = alpha_cities, aes(x = long, y = lat), color = 'red')

</pre>
<p><a rel="nofollow" href="http://sharpsightlabs.com/join-newsletter-rapid-mastery/" target="_blank"><br>
<img sizes="(max-width: 600px) 100vw, 600px" src="https://i1.wp.com/sharpsightlabs.com/wp-content/uploads/2017/11/alpha_cities-600x438.png?w=450" alt="" class="wp-image-3167" data-recalc-dims="1"><br>
</a></p>
<p>… and now we’ll use the faceting technique to break out our plot using the  variable.</p>
<pre class="wp-code-highlight">
#==========================
# CREATE SMALL MULTIPLE MAP
#==========================

ggplot() +
  geom_polygon(data = map_world, aes(x = long, y = lat, group = group)) +
  geom_point(data = alpha_cities, aes(x = long, y = lat), color = 'red') +
  #facet_grid(. ~ rating)
  #facet_grid(rating ~ .)
  facet_wrap(~ rating)

</pre>
<p><a href="http://sharpsightlabs.com/join-newsletter-rapid-mastery/"><br>
<img sizes="(max-width: 600px) 100vw, 600px" src="https://i1.wp.com/sharpsightlabs.com/wp-content/uploads/2017/11/alpha_cities_faceted_2-600x492.png?w=450" alt="" class="wp-image-3166" data-recalc-dims="1"><br>
</a></p>
<p>Once again, this is a good example of an intermediate-level project that you could do to practice your data wrangling and data visualization skills.</p>
<p>Having said that, before you attempt to do something like this yourself, I highly recommend that you first master the individual tools that we used here (i.e., the tools from , , and the tidyverse).</p>
<h1>Sign up now, and discover how to rapidly master data science</h1>
<p>To master data science, you need to master the essential tools.</p>
<p>And to make rapid progress, you need to know what to learn, what <em>not</em> to learn, and you need to know <em>how to practice</em> what you learn.</p>
<p>Sharp Sight is dedicated to teaching you how to master the tools of data science as quickly as possible.</p>
<p><a rel="nofollow" href="http://sharpsightlabs.com/join-newsletter-rapid-mastery/" target="_blank">Sign up now for our email list</a>, and you’ll receive regular tutorials and lessons. </p>
<p>You’ll learn:</p>
<ul>
<li>What data science tools you should learn (and what not to learn)</li>
<li>How to practice those tools</li>
<li>How to put those tools together to execute analyses and machine learning projects</li>
<li>… and more </li>
</ul>
<p>If you <a rel="nofollow" href="http://sharpsightlabs.com/join-newsletter-rapid-mastery/" target="_blank">sign up for our email list <em>right now</em></a>, you’ll also get access to our “Data Science Crash Course” <u>for free</u>.</p>
<p><a rel="nofollow" href="http://sharpsightlabs.com/join-newsletter-rapid-mastery/" target="_blank">
<p><strong>SIGN UP NOW</strong></p>
<p></p></a>
<p>The post <a rel="nofollow" href="http://sharpsightlabs.com/blog/mapping-world-cities/" target="_blank">Mapping “world cities” in R</a> appeared first on <a rel="nofollow" href="http://sharpsightlabs.com/" target="_blank">SHARP SIGHT LABS</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://sharpsightlabs.com/blog/mapping-world-cities/"> r-bloggers – SHARP SIGHT LABS</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/qAIdNKPXu3M" height="1"></a></div>