Visualizing Average Rainfall in the US by State

The inspiration for this post came to me while randomly browsing data sets on the internet. I found this page and decided to practice using ggplot2 to make heat maps. First, I had to transfer the data from the table on the website into a .csv file in Excel so I could properly import the data into R.

The code itself is very short which shows just how powerful and efficient ggplot2 really is.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
library(ggplot2)
library(datasets)
library(maps)
library(mapproj)

rain.data = read.csv("rain.csv", header=T)
states <- map_data("state")
rain.data$region <- tolower((rain.data$State))

merged <- merge(states, rain.data, sort=FALSE, by = "region")
merged <- merged[order(merged$order), ]

qplot(long, lat, data = merged, group = group, fill = Inches, geom = "polygon") +
  opts(title = "Average Rainfall in the US from 1971-2000")

I love that ggplot2 includes a legend for the graph by default.