Book Image

R Data Visualization Cookbook

Book Image

R Data Visualization Cookbook

Overview of this book

Table of Contents (17 chapters)
R Data Visualization Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Generating a very basic pyramid plot


You might have seen these plots in news or journal articles and wondered how to create them quickly. This recipe will help you accomplish this task. Pyramid plots are horizontal bar plots and they are often used to display gender differences in a dataset. I have created this plot based on a New York Times infographic discussing the deaths by different types of cancer among men and women. The data was extracted from the Centers of Disease Control and Prevention website.

Getting ready

We require the following packages:

  • plotrix

  • RColorBrewer

How to do it…

The install.packages() and library() functions can be used to install and load the packages in R:

install.packages(c("plotrix","RColorBrewer"))
library("plotrix")
library("RColorBrewer")

The read.csv() function is used to load the CSV file in R:

data = read.csv("cancer.csv", sep = ",", header = TRUE)

Next, we can create a pyramid plot in R using the pyramid.plot() function and add the legends using the legend...