How to make a line chart with ggplot2
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Last week’s blog post about Amazon’s search for a location for a second headquarters left me thinking about the company’s growth.
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 how to create a line chart in R using
So in this blog post, I’ll show you how to make a line chart with
Let’s jump in.
First, we’ll load several packages: we’ll load
#=============== # LOAD PACKAGES #=============== library(readr) library(tidyverse) library(stringr)
Now that we’ve loaded the packages that we need, we’ll read in the data.
The data are contained in a
We’ll use
#========== # READ DATA #========== stock_amzn <- read_csv("http://sharpsightlabs.com/wp-content/uploads/2017/09/AMZN_stock.csv")
Now we’ll quickly inspect the data by looking at the column names and printing out the first few rows of data.
#======== # INSPECT #======== stock_amzn %>% names() stock_amzn %>% head()
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.
To convert the variable names to all lower case, we’ll use the
#========================================================= # 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) <- colnames(stock_amzn) %>% str_to_lower() # inspect stock_amzn %>% names()
Here, on the right-hand-side of the assignment operator, we’re using
The resulting output is then re-assigned to the column names of the dataframe. We do this by using the following:
Now that the data are in the right form, let’s make a simple line chart.
#====== # PLOT #====== #-------------------------------------- # FIRST ITERATION # - this is the quick-and-dirty version #-------------------------------------- ggplot(data = stock_amzn, aes(x = date, y = close)) + geom_line()
This is about as simple as it gets in
The
The
Then, the
Finally,
Again, this is just about as simple as it gets.
Once you know more about how
Having said that, let’s take a look at a ‘polished’ version of the plot … a version that’s been heavily formatted:
#-------------------------------------- # 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 = "Amazon's stock price has increased dramatically\nover the last 20 years") + theme(text = element_text(family = 'Gill Sans', color = "#444444") ,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) )
And here’s the final chart:
If you’re a beginner, don’t be intimidated: this finalized chart is not hard to do.
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 learn how the code works, and you should be able to memorize this syntax within a week or two.
… and when I say memorize, I mean that you should be able to write all of this code from memory.
Ideally, if you’re fluent in
Sign up now, and discover how to become fluent in R
Are you still struggling with R and
Becoming fluent in R is really straightforward, if you know how to practice.
If you’re ready to master R, sign up for our email list.
Not only will you receive tutorials (delivered to your inbox) …
… but you’ll also get lessons on how to practice so that you can master R as quickly as possible.
And if you sign up right now, you’ll also get access to our “Data Science Crash Course” for free.
SIGN UP NOW
The post How to make a line chart with
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.