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

Setting colors of points, lines, and bars


In this recipe, you will learn the simplest way to change the colors of points, lines, and bars in scatter plots, line plots, histograms, and bar plots.

Getting ready

All you need to try out in this recipe is to run R and type the recipe in the command prompt. You can also choose to save the recipe as a script so that you can use it again later on.

How to do it...

The simplest way to change the color of any graph element is using the col argument. For example, the plot() function takes the col argument:

plot(rnorm(1000),
col="red")

If we choose the plot type as the line, then the color is applied to the plotted line. Let's use the dailysales.csv example dataset we used in Chapter 1, R Graphics. First, we need to load it:

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

plot(sales$units~as.Date(sales$date,"%d/%m/%y"),
type="l", #Specify type of plot as l for line
col="blue")

Similarly, the points() and lines() functions apply the col argument's value...