Book Image

R Graphs Cookbook Second Edition

Book Image

R Graphs Cookbook Second Edition

Overview of this book

Table of Contents (22 chapters)
R Graphs Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Creating line graphs


Line graphs are generally used to look at trends in data over time, so the x variable is usually time expressed as time of day, date, month, year, and so on. In this recipe, we will see how we can quickly plot such data using the same plot() function that was used in the previous recipe to make scatter plots.

Getting ready

First, we need to load the dailysales.csv example data file (you can download this file from the code download section of the book's companion website):

sales<-read.csv("dailysales.csv", header=TRUE)

As the file's name suggests, it contains daily sales data of a product. It has two columns: a date column and a sales column that shows the number of units sold.

How to do it...

Here's the code to make your first line graph:

plot(sales$units~as.Date(sales$date,"%d/%m/%y"),
type="l", #Specify type of plot as l for line
main="Unit Sales in the month of January 2010",
xlab="Date",
ylab="Number of units sold",
col="blue")

How it works...

We first read the data file...