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 a line chart


Line charts are sometimes useful in order to view the pattern of a single variable against its index. Also, in some cases, bivariate connected lines are also useful. In this recipe, we will draw a line chart using ggplot2 with geom_line.

Getting ready

Once again, we recall ggplotdata for this plot. In this case, we will use the two numeric variables, which are disA and disD.

How to do it...

To produce a connected line of a single numeric variable against its observation index, we will use the following code:

# connected line
ggplot(data=ggplotdata,aes(x=1:nrow(ggplotdata),y=disA))+geom_line()

The line chart produced is shown in the following figure:

How it works…

The single variable's connected line looks like a time series plot, but the difference is that this plot does not have the time axis. The x argument within aes() specifies the values of the x-axis. In this case, the value of x-axis is just the sequence number starting from 1 to the number of observations in the dataset...