<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/how-to-make-a-line-chart-with-ggplot2/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://sharpsightlabs.com/blog/line-chart-ggplot2-amzn/"> 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 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/09/amzn_stock_price_NO-TITLE-600x277.png?w=450" alt="" class="wp-image-3133" data-recalc-dims="1"><br>
</a></p>
<p><a href="http://sharpsightlabs.com/blog/new-amazon-hq/">Last week’s blog post</a> about Amazon’s search for a location for a second headquarters left me thinking about the company’s growth.</p>
<p>After looking at the long term growth of the stock price, it occurred to me that visualizing the stock price data would be a great example of <a href="http://sharpsightlabs.com/blog/line-chart-in-r-basic/">how to create a line chart in R</a> using .</p>
<p>So in this blog post, I’ll show you how to make a line chart with , step by step.</p>
<p>Let’s jump in.</p>
<p>First, we’ll load several packages: we’ll load , which we’ll use to read in the data; , which includes , <a href="https://www.r-bloggers.com/how-to-make-a-line-chart-with-ggplot2/sharpsightlabs.com/blog/dplyr-intro-data-manipulation-with-r/"></a>, and several other important packages; and , which will let us do some string manipulation.</p>
<pre class="wp-code-highlight">
#===============
# LOAD PACKAGES
#===============

library(readr)
library(tidyverse)
library(stringr)

</pre>
<p>Now that we’ve loaded the packages that we need, we’ll read in the data.</p>
<p>The data are contained in a  file that I’ve uploaded to the Sharp Sight webpage. </p>
<p>We’ll use  to read in the file. This is an extremely straightforward use of .</p>
<pre class="wp-code-highlight">
#==========
# READ DATA
#==========

stock_amzn &lt;- read_csv(&quot;http://sharpsightlabs.com/wp-content/uploads/2017/09/AMZN_stock.csv&quot;)

</pre>
<p>Now we’ll quickly inspect the data by looking at the column names and printing out the first few rows of data.</p>
<pre class="wp-code-highlight">
#========
# INSPECT
#========

stock_amzn %>% names()
stock_amzn %>% head()

</pre>
<p>Upon inspection, you can see that the column names are capitalized. This is a minor problem, but ideally you want your variable names to be lower case; this makes them easier to type.</p>
<p>To convert the variable names to all lower case, we’ll use the  function from the  package.</p>
<pre class="wp-code-highlight">
#=========================================================
# CHANGE COLUMN NAMES: lower case
# - in the raw form (as read in) the first letter of 
#   each variable is capitalized. 
# - This makes them harder to type!  Not ideal.
# - we'll use stringr::str_to_lower() to change the column
#   names to lower case 
#=========================================================

colnames(stock_amzn) &lt;- colnames(stock_amzn) %>% str_to_lower()

# inspect
stock_amzn %>% names()

</pre>
<p>Here, on the right-hand-side of the assignment operator, we’re using  to <em>retrieve</em> the column names. Then we pipe the column names into  which converts the names to lower case.</p>
<p>The resulting output is then re-assigned to the column names of the dataframe. We do this by using the following: . Essentially, we’re taking the result from the right-hand-side and assigning that result to the column names using . To be clear,  can both <em>retrieve</em> column names and <em>set</em> the column names. </p>
<p>Now that the data are in the right form, let’s make a simple line chart.</p>
<pre class="wp-code-highlight">
#======
# PLOT 
#======

#--------------------------------------
# FIRST ITERATION
# - this is the quick-and-dirty version
#--------------------------------------

ggplot(data = stock_amzn, aes(x = date, y = close)) +
  geom_line()

</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://i0.wp.com/sharpsightlabs.com/wp-content/uploads/2017/09/amzn_stock_price_NO-FORMAT-600x342.png?w=450" alt="" class="wp-image-3135" data-recalc-dims="1"><br>
</a></p>
<p>This is about as simple as it gets in , but let’s break it down.</p>
<p>The  function indicates that we’re going to use  to make a plot.</p>
<p>The  parameter specifies that we’re going to be plotting data in the  dataframe.</p>
<p>Then, the  function allows us to specify our variable mappings. With the statement , we are mapping the  variable to the x-axis. Similarly, with the statement , we are mapping the  variable to the y-axis.</p>
<p>Finally,  specifies that we want to draw <em>lines</em>.</p>
<p>Again, this is just about as simple as it gets.</p>
<p>Once you know more about how  works, <a href="http://sharpsightlabs.com/blog/format-titles-and-axes-in-ggplot2/">you can format the plot</a>.</p>
<p>Having said that, let’s take a look at a ‘polished’ version of the plot … a version that’s been heavily formatted:</p>
<pre class="wp-code-highlight">
#--------------------------------------
# POLISHED VERSION
# - this is the 'finalized' version
# - we arrive at this after a lot of
#   itteration ....
#--------------------------------------

ggplot(stock_amzn, aes(x = date, close)) +
  geom_line(color = 'cyan') +
  geom_area(fill = 'cyan', alpha = .1) +
  labs(x = 'Date'
       , y = 'Closing\nPrice'
       , title = &quot;Amazon's stock price has increased dramatically\nover the last 20 years&quot;) +
  theme(text = element_text(family = 'Gill Sans', color = &quot;#444444&quot;)
        ,panel.background = element_rect(fill = '#444B5A')
        ,panel.grid.minor = element_line(color = '#4d5566')
        ,panel.grid.major = element_line(color = '#586174')
        ,plot.title = element_text(size = 28)
        ,axis.title = element_text(size = 18, color = '#555555')
        ,axis.title.y = element_text(vjust = 1, angle = 0)
        ,axis.title.x = element_text(hjust = 0)
        ) 
</pre>
<p>And here’s the final chart:</p>
<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/09/amazon_stock_price_v2-600x345.png?w=450" alt="" class="wp-image-3134" data-recalc-dims="1"><br>
</a></p>
<p>If you’re a beginner, don’t be intimidated: this finalized chart is <em>not</em> hard to do.</p>
<p>Really. With a little practice, you should be able to learn to create a well-formatted chart like this very quickly. It should take you only a few hours to <em>learn how the code works</em>, and you should be able to memorize this syntax within a week or two.</p>
<p>… and when I say memorize, I mean that you should be able to write all of this code <em>from memory</em>. </p>
<p>Ideally, if you’re fluent in  and , it should only take you 10 or 15 minutes to write all of this code, start to finish.</p>
<h1>Sign up now, and discover how to become fluent in R</h1>
<p>Are you still struggling with R and ?</p>
<p>Becoming fluent in R is really straightforward, if you know how to practice.</p>
<p>If you’re ready to master R, <a rel="nofollow" href="http://sharpsightlabs.com/join-newsletter-rapid-mastery/" target="_blank">sign up for our email list</a>.</p>
<p>Not only will you receive tutorials (delivered to your inbox) …</p>
<p>… but you’ll also get lessons on <em>how to practice</em> so that you can master R as quickly as possible.</p>
<p>And if you <a rel="nofollow" href="http://sharpsightlabs.com/join-newsletter-rapid-mastery/" target="_blank">sign up <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/line-chart-ggplot2-amzn/" target="_blank">How to make a line chart with </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/line-chart-ggplot2-amzn/"> 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/vb19OYAmqFc" height="1"></a></div>