Book Image

R Graphs Cookbook

By : Hrishi V. Mittal
Book Image

R Graphs Cookbook

By: Hrishi V. Mittal

Overview of this book

<p>With more than two million users worldwide, R is one of the most popular open source projects. It is a free and robust statistical programming environment with very powerful graphical capabilities. Analyzing and visualizing data with R is a necessary skill for anyone doing any kind of statistical analysis, and this book will help you do just that in the easiest and most efficient way possible.</p> <p>Unlike other books on R, this book takes a practical, hands-on approach and you dive straight into creating graphs in R right from the very first page.</p> <p>You want to harness the power of this open source programming language to visually present and analyze your data in the best way possible – and this book will show you how.</p> <p>The <em>R Graph Cookbook</em> takes a practical approach to teaching how to create effective and useful graphs using R. This practical guide begins by teaching you how to make basic graphs in R and progresses through subsequent dedicated chapters about each graph type in depth. It will demystify a lot of difficult and confusing R functions and parameters and enable you to construct and modify data graphics to suit your analysis, presentation, and publication needs.</p> <p>You will learn all about making graphics such as scatter plots, line graphs, bar charts, pie charts, dot plots, heat maps, histograms and box plots. In addition, there are detailed recipes on making various combinations and advanced versions of these graphs. Dedicated chapters on polishing and finalizing graphs will enable you to produce professional-quality graphs for presentation and publication. With R Graph Cookbook in hand, making graphs in R has never been easier.</p>
Table of Contents (16 chapters)
R Graphs Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface

Formatting time series data for plotting


Time series or trend charts are the most common form of line graphs. There are a lot of ways in R to plot such data, however it is important to first format the data in a suitable format that R can understand. In this recipe, we will look at some ways of formatting time series data using the base and some additional packages.

Getting ready

In addition to the basic R functions, we will also be using the zoo package in this recipe. So first we need to install it:

install.packages("zoo")

How to do it...

Let's use the dailysales.csv example dataset and format its date column:

sales<-read.csv("dailysales.csv")
d1<-as.Date(sales$date,"%d/%m/%y")
d2<-strptime(sales$date,"%d/%m/%y")
data.class(d1)
[1] "Date"
data.class(d2)
[1] "POSIXt"

How it works...

We have seen two different functions to convert a character vector into dates. If we did not convert the date column, R would not automatically recognize the values in the column as dates. Instead, the column...