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 plot background colors


The default background color of all plots in R is white, which is usually the best choice as it is least distracting for data analysis. However, sometimes, we might wish to use another color. We will see how to set background colors in this recipe.

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...

To set the plot background color to gray, we use the bg argument in the par() command:

par(bg="gray")
plot(rnorm(100))

How it works...

The bg argument of the par() command sets the background color for the entire plotting area, including the margins for any subsequent plots on the same device. Until the plotting device is closed or a new device is initiated, the background color stays the same.

There's more...

It is more likely that we want to set the background color only for the plot region (within the axes) but...