<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-lithium-production-using-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://sharpsightlabs.com/blog/map-lithium-production-r/"> 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://i0.wp.com/sharpsightlabs.com/wp-content/uploads/2017/12/lithium_resources_NOLEGEND-600x355.png?w=450" alt="" class="wp-image-3189" data-recalc-dims="1"><br>
</a></p>
<p>A great thing about data science – and <a href="https://www.r-bloggers.com/mapping-lithium-production-using-r/sharpsightlabs.com/blog/start-with-data-visualization-manipulation/">data visualization in particular</a> – is that you can use data science tools to get information about a subject very quickly. </p>
<p>This is particularly true when you know how to <em>present</em> the information. When you know the best technique for visualizing data, you can use those tools to present information in a way that can help you increase your understanding very quickly.</p>
<p>Here’s a case in point: I was thinking about energy, solar energy, and battery technology, and I got curious about sources of lithium (which is used in batteries and related tech).</p>
<p>Using a few tools from , I created a map of lithium production within about 15 minutes. The maps that I created certainly don’t tell the full story, but they at least provide a baseline of knowledge.</p>
<p>If you’re <a href="http://sharpsightlabs.com/blog/are-you-fluent-r/">fluent with the tools and techniques</a> of data science, this becomes possible. Whether you’re just doing some personal research, or working for a multinational corporation, you can use the tools of data science to quickly <a href="http://sharpsightlabs.com/blog/data-visualizations-are-tools-and-what-youre-building/">identify and present insights</a> about the world.</p>
<p>Let me walk you through this example and show you how I did it.</p>
<h2>Tutorial: how to map lithium production data using R</h2>
<p>First, we’ll load the packages that we’re going to use.</p>
<p>We’ll load  mostly for access to  and , but we’ll also load  (for data scraping),  (to help us <a href="http://sharpsightlabs.com/blog/shipping-analysis-r-data-wrangling/">manipulate our variables and put the data into shape</a>), and  (to help us <a href="http://sharpsightlabs.com/blog/quick-intro-color-density-plot/">modify the color of the final plot</a>).</p>
<pre class="wp-code-highlight">
#--------------
# LOAD PACKAGES
#--------------

library(tidyverse)
library(rvest)
library(stringr)
library(viridis)

</pre>
<p>Ok. Now, we’re going to scrape this mineral production data from the wikipedia page.</p>
<p>Notice that we’re essentially using several  functions in series. We’re using several functions and <a href="https://www.r-bloggers.com/mapping-lithium-production-using-r/sharpsightlabs.com/blog/dplyr-intro-data-manipulation-with-r/">combining them together</a> using the “pipe” operator, .</p>
<p>If you’re not using it yet, you should definitely familiarize yourself with this operator, and start using it. It’s beyond the scope of this blog post to talk extensively about pipe operator, but I will say that it’s one of the most useful tools in the  data scientist’s toolkit. If you learn to use it properly, it will make your code easier to read and easier to write. It will even train your mind to think about analysis in a more step by step way. </p>
<p>Concerning the code: what we’re doing here is designating the URL from which we’re going to scrape the data, then we’re specifying that we’ll be scraping one of the tables. Then, we specify that we’re going to scrape data from the 9th table, and finally we coerce the data into a  (instead of keeping it as a traditional ). </p>
<pre class="wp-code-highlight">
#---------------------------
# SCRAPE DATA FROM WIKIPEDIA
#---------------------------

df.lithium &lt;- read_html(&quot;https://en.wikipedia.org/wiki/Lithium&quot;) %>%
  html_nodes(&quot;table&quot;) %>%
  .[[9]] %>%
  html_table() %>%
  as.tibble()



# INSPECT
df.lithium

</pre>
<p>The resultant dataset, , is relatively small (which makes it easy to inspect and easy to work with), but in its raw form, it’s a little messy. We’ll need to do a few things to clean up the data, like change the variable names, parse some data into numerics, etc.</p>
<p>So first, let’s change the column names.</p>
<p>There are a few ways we could do this, but the most straightforward is to simply pass a vector of manually-defined column names into the  function.</p>
<pre class="wp-code-highlight">
#--------------------------------------------
# CHANGE COLUMN NAMES
# - the raw column names are capitalized and 
#   have some extra information
# - we will just clean them up
#--------------------------------------------

colnames(df.lithium) &lt;- c('country', 'production', 'reserves', 'resources')

colnames(df.lithium)


</pre>
<p>Now, we’ll remove an extraneous row of data. The original data table on Wikipedia contained not only the individual records of lithium production for particular countries, but it also contained a “total” row at the bottom of the table. More often than not, these sorts of “total” rows are not appropriate for a  in ; we’ll typically remove them, just as we will do here.</p>
<pre class="wp-code-highlight">
#-----------------------------------------------
# REMOVE &quot;World total&quot;
# - this is a total amount that was
#   in the original data table
# - we need to remove, because it's not a
#   proper data record for a particular country
#-----------------------------------------------

df.lithium &lt;- df.lithium %>% filter(country != 'World total')

df.lithium

</pre>
<p>Next, we need to parse the numbers into actual numeric data. The reason is that when we scraped the data, it actually read in the numbers as character data, along with commas and some extra characters. We need to transform this character data into proper numeric data in .</p>
<p>To do this, we’ll need to do a few things. First, we need to remove a few “notes” that were in the original data. This is why we’re using the code . Essentially, we’re removing those notes from the data.</p>
<p>After that, we’re using  to transform three variables – , ,  – into numerics.</p>
<p>Note once again how we’re structuring this code. We’re using a combination of <a href="https://www.r-bloggers.com/mapping-lithium-production-using-r/sharpsightlabs.com/blog/dplyr-intro-data-manipulation-with-r/">functions from </a> and  to achieve our objectives.</p>
<p>To a beginner, this might look complicated, but it’s really not that bad once you understand the individual pieces. If you don’t understand this code (our couldn’t write it yourself), I recommend that you <a href="https://www.r-bloggers.com/mapping-lithium-production-using-r/sharpsightlabs.com/blog/dplyr-intro-data-manipulation-with-r/">learn the individual functions from </a> and  first, and then come back to this once you’ve learned those pieces.</p>
<pre class="wp-code-highlight">
#---------------------------------------------------------
# PARSE NUMBERS
# - the original numeric quantities in the table
#   were read-in as character data
# - we need to &quot;parse&quot; this information ....
#   &amp; transform it from character into proper numeric data
#---------------------------------------------------------

# Strip out the 'notes' from the numeric data
#str_replace(df.lithium$production,&quot;W\\[.*\\]&quot;, &quot;&quot;) #test

df.lithium &lt;- df.lithium %>% mutate(production = str_replace(production,&quot;W\\[.*\\]&quot;, &quot;-&quot;))

# inspect
df.lithium


# Parse character data into numbers
df.lithium &lt;- df.lithium %>% mutate(production = parse_number(production, na = '-')
                   ,reserves = parse_number(reserves, na = '-')
                   ,resources = parse_number(resources, na = '-')
                     )

# Inspect
df.lithium

</pre>
<p>Now we’ll get data for a map of the world. To do this, we’ll just use .</p>
<pre class="wp-code-highlight">
#--------------
# GET WORLD MAP
#--------------

map.world &lt;- map_data('world')

</pre>
<p>We’ll also get the names of the countries in this dataset. </p>
<p>The reason is because we’ll need to join this map data to the data from Wikipedia, and we’ll need the country names to be exactly the same. To make this work, we’ll need to examine the names in both datasets and modify any names that aren’t exactly the same.</p>
<p>Notice that once again, we’re using a combination of functions from , wiring them together using the pipe operator.</p>
<pre class="wp-code-highlight">
#----------------------------------------------------
# Get country names
# - we can use this list and cross-reference
#   with the country names in the scraped data
# - when we find names that are not the same between
#   this map data and the scraped data, we can recode
#   the values
#----------------------------------------------------

map_data('world') %>% group_by(region) %>% summarise() %>% print(n = Inf)


</pre>
<p>Ok. Now we’re going to recode some country names. Again, we’re going this so that the country names in  are the same as the corresponding country names in .</p>
<pre class="wp-code-highlight">
#--------------------------------------------
# RECODE COUNTRY NAMES
# - some of the country names do not match
#   the names we will use later in our map
# - we will re-code so that the data matches
#   the names in the world map
#--------------------------------------------

df.lithium &lt;- df.lithium %>% mutate(country = if_else(country == &quot;Canada (2010)&quot;, 'Canada'
                                      ,if_else(country == &quot;People's Republic of China&quot;, &quot;China&quot;
                                      ,if_else(country == &quot;United States&quot;, &quot;USA&quot;
                                      ,if_else(country == &quot;DR Congo&quot;,&quot;Democratic Republic of the Congo&quot;, country))))
                                )


# Inspect
df.lithium

</pre>
<p>Ok, now we’ll join the data using .</p>
<pre class="wp-code-highlight">
#-----------------------------------------
# JOIN DATA
# - join the map data and the scraped-data
#-----------------------------------------

df &lt;- left_join(map.world, df.lithium, by = c('region' = 'country'))

</pre>
<p>Now we’ll plot.</p>
<p>We’ll start with just a basic plot (to make sure that the map plots correctly), and then we’ll proceed to plot separate maps where the fill color corresponds to , , and .</p>
<pre class="wp-code-highlight">
#-----------
# PLOT DATA
#-----------

# BASIC MAP
ggplot(data = df, aes(x = long, y = lat, group = group)) +
    geom_polygon()


# LITHIUM RESERVES
ggplot(data = df, aes(x = long, y = lat, group = group)) +
  geom_polygon(aes(fill = reserves))


ggplot(data = df, aes(x = long, y = lat, group = group)) +
  geom_polygon(aes(fill = reserves)) +
  scale_fill_viridis(option = 'plasma')


# LITHIUM PRODUCTION
ggplot(data = df, aes(x = long, y = lat, group = group)) +
  geom_polygon(aes(fill = production)) +
  scale_fill_viridis(option = 'plasma')


# LITHIUM RESOURCES
ggplot(data = df, aes(x = long, y = lat, group = group)) +
  geom_polygon(aes(fill = resources)) +
  scale_fill_viridis(option = 'plasma')

</pre>
<p><a href="http://sharpsightlabs.com/join-newsletter-rapid-mastery/"><br>
<img sizes="(max-width: 600px) 100vw, 600px" src="https://i0.wp.com/sharpsightlabs.com/wp-content/uploads/2017/12/lithuim_reserves-600x399.png?w=450" alt="" class="wp-image-3188" data-recalc-dims="1"><br>
</a></p>
<p><a href="http://sharpsightlabs.com/join-newsletter-rapid-mastery/"><br>
<img sizes="(max-width: 600px) 100vw, 600px" src="https://i0.wp.com/sharpsightlabs.com/wp-content/uploads/2017/12/lithium_production-600x401.png?w=450" alt="" class="wp-image-3187" data-recalc-dims="1"><br>
</a></p>
<p><a href="http://sharpsightlabs.com/join-newsletter-rapid-mastery/"><br>
<img sizes="(max-width: 600px) 100vw, 600px" src="https://i2.wp.com/sharpsightlabs.com/wp-content/uploads/2017/12/lithium_resources-600x401.png?w=450" alt="" class="wp-image-3186" data-recalc-dims="1"><br>
</a></p>
<p>In the final three versions, notice as well that we’re modifying the color scales by using .</p>
<p>There’s actually <a href="https://www.r-bloggers.com/mapping-lithium-production-using-r/sharpsightlabs.com/blog/format-titles-and-axes-in-ggplot2/">quite a bit more formatting</a> that we could do on these, but as a first pass, these are pretty good. </p>
<p>I’ll leave it as an exercise for you to format these with titles, background colors, etc. If you choose to do this, leave your finalized code in the comments section below.</p>
<h1>To master data science, you need a plan</h1>
<p>At several points in this tutorial, I’ve mentioned a high level plan for mastering data science: master individual pieces of a programming language, and then learn to put them together into more complicated structures. </p>
<p>If you can do this, you will accelerate your progress … although, the devil is in the details.</p>
<p>That’s actually not the only learning hack that you can use to rapidly master data science. There are lots of other tricks and learning hacks that you can use to dramatically accelerate your progress.</p>
<p>Want to know them?</p>
<p>Sign up for <a href="http://sharpsightlabs.com/join-newsletter-rapid-mastery/">our email list</a>.</p>
<p>Here at Sharp Sight, we teach data science. But we also teach you <em>how to learn</em> and <em>how to study</em> data science, so you master the tools as quickly as possible.</p>
<p>By signing up for our email list, you’ll get weekly tutorials about data science, delivered directly to your inbox.</p>
<p>You’ll also get our <a href="http://sharpsightlabs.com/join-newsletter-rapid-mastery/">Data Science Crash Course</a>, <em>for free</em>.</p>
<p><a href="http://sharpsightlabs.com/join-newsletter-rapid-mastery/">
<p><strong>SIGN UP NOW</strong></p>
<p></p></a>
<p>The post <a rel="nofollow" href="http://sharpsightlabs.com/blog/map-lithium-production-r/" target="_blank">Mapping lithium production using 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/map-lithium-production-r/"> 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/xnemt0k-jXM" height="1"></a></div>