suppressPackageStartupMessages(library(ggplot2))
library(plyr)

usmap <- map_data("county")

## Step 1 - select Wisconsin data
mwmap <- usmap[usmap$region == "wisconsin", ]
mwdata <- midwest[midwest$state == "WI", ]
mwdata[, 2] <- tolower(mwdata[, 2])

mwdata$percadult <- round(100 * mwdata[, 17]/mwdata[, 5], digits = 2)
## histograms (1D distributions)
hh <- ggplot(data = mwdata, aes(poptotal))
hh + stat_bin(bins = 10, color = "red", fill = I("darkorange"))

hh <- ggplot(data = mwdata, aes(percwhite))
hh + stat_bin(binwidth = 5, color = "blue", fill = I("skyblue"))

hh <- ggplot(data = mwdata, aes(percollege))
hh + stat_bin(binwidth = 5, color = "blue", fill = I("yellow"))

hh <- ggplot(data = mwdata, aes(percbelowpoverty))
hh + stat_bin(binwidth = 5, color = "violet", fill = I("pink"))

hh <- ggplot(data = mwdata, aes(percadult))
hh + stat_bin(binwidth = 5, color = "darkgreen", fill = I("green"))

bb <- ggplot(data = mwdata, aes(percwhite, percollege))
bb + stat_bin2d(bins = 9, color = "red") +
  scale_fill_gradient(low = "orange", high = "darkgreen")

bb <- ggplot(data = mwdata, aes(percollege, percbelowpoverty))
bb + stat_bin2d(bins = 5, color = "blue") +
    scale_fill_gradient(low = "green", high = "purple")

## rename the field subregion as region
names(mwmap)[5:6] <- c("state", "region")

## plot the variable "percollege"
mwp <- ggplot(data = mwdata, aes(fill = percollege, 
                               colour = I("blue")))
mwp <- mwp + scale_fill_gradient(low = "orange", 
                                 high = "darkgreen")
mwp <- mwp + geom_map(aes(map_id = county), map = mwmap) + expand_limits(x = mwmap$long, y = mwmap$lat)
mwp <- mwp + coord_fixed(ratio = 1.25) + theme_bw()
mwp <- mwp + labs(title = "Wisconsin", x = "", y = "", 
                  fill = "percollege")
mwp

library(RColorBrewer)
## Warning: package 'RColorBrewer' was built under R version 3.1.3
a <- cut(mwdata$percwhite, 
         breaks = c(10,75,80,85,90,95,100), dig.lab = 3)
mwdata$percwhite_grp <- a
## plot the variable "percwhite"
mwp <- ggplot(data = mwdata, aes(fill = percwhite_grp, 
                                 colour = I("red")))
mwp <- mwp + scale_fill_brewer(palette = "YlGnBu")

mwp <- mwp + geom_map(aes(map_id = county), map = mwmap) + expand_limits(x = mwmap$long, y = mwmap$lat)
mwp <- mwp + coord_fixed(ratio = 1.25) + theme_bw()
mwp <- mwp + labs(title = "Wisconsin", x = "", y = "", 
                  fill = "percwhite")
mwp

b <- cut(mwdata$percbelowpoverty, 
         breaks = c(0,5,10,15,20,100), dig.lab = 3)
mwdata$percbelowpoverty_grp <- b
## plot the variable "percbelowpoverty"
mwp <- ggplot(data = mwdata, aes(fill = percbelowpoverty_grp, colour = I("red")))
mwp <- mwp + scale_fill_brewer(palette = "Purples")

mwp <- mwp + geom_map(aes(map_id = county), map = mwmap) + expand_limits(x = mwmap$long, y = mwmap$lat)
mwp <- mwp + coord_fixed(ratio = 1.25) + theme_bw()
mwp <- mwp + labs(title = "Wisconsin", x = "", y = "", 
                  fill = "percbelowpoverty")
mwp

b2 <- cut(mwdata$percadult, 
          breaks = c(45,60,64,68,72,75), dig.lab = 3)
mwdata$percadult_grp <- b2
## plot the variable "percadult"
mwp <- ggplot(data = mwdata, aes(fill = percadult_grp, colour = I("blue")))
mwp <- mwp + scale_fill_brewer(palette = "Reds")

mwp <- mwp + geom_map(aes(map_id = county), map = mwmap) + expand_limits(x = mwmap$long, y = mwmap$lat)
mwp <- mwp + coord_fixed(ratio = 1.25) + theme_bw()
mwp <- mwp + labs(title = "Wisconsin", x = "", y = "", 
                  fill = "percadult")
mwp

b3 <- cut(mwdata$poptotal, 
          breaks = c(0,2e4,5e4,1e5,2e5,5e5,1e6), dig.lab = 7)
mwdata$poptotal_grp <- b3
## plot the variable "poptotal"
mwp <- ggplot(data = mwdata, aes(fill = poptotal_grp, colour = I("blue")))
mwp <- mwp + scale_fill_brewer(palette = "Greys")

mwp <- mwp + geom_map(aes(map_id = county), map = mwmap) + expand_limits(x = mwmap$long, y = mwmap$lat)
mwp <- mwp + coord_fixed(ratio = 1.25) + theme_bw()
mwp <- mwp + labs(title = "Wisconsin", x = "", y = "", 
                  fill = "poptotal")
mwp