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 graphs with regional maps


In this recipe, we will learn how to plot data on regional maps within individual countries rather than the whole world map. We will look at examples based on the United States and European countries.

Getting ready

Just as with the previous recipe, we will make use of the maps package to draw the map and the RColorBrewer package to choose color schemes. So, let's make sure that they are loaded:

library(maps)
library(RColorBrewer)

We will use the inbuilt USArrests example dataset, which contains crime statistics, in arrests per 100,000 residents for assault, murder, and rape in each of the 50 US states in 1973.

How to do it...

Let's plot the arrests rate for murders in US states in 1973. The default graphics device size might not be big enough for the map; thus, if you get an error about figure margins, enlarge the graphics device:

x<-map("state",plot=FALSE)

for(i in 1:length(rownames(USArrests))) {
    for(j in 1:length(x$names)) {
        if(grepl(rownames...