The following work is an attempt to display the results of a data analysis making use of the powerful functions of the ggplot2 package in R.
The data source is the US State Facts and Figures data set from R.
library(ggplot2)
colnames(state.x77) <- gsub(" ", "", colnames(state.x77))
statedf <- data.frame(state.abb, state.area, state.center, state.name, state.region, state.x77)
statedf <- statedf[state.abb != "AK" & state.abb != "HI", ]
bb <- ggplot(data = statedf, aes(state.region, Income))
bb <- bb + geom_boxplot(aes(fill = state.region)) + coord_flip()
bb <- bb + labs(title = "US per capita Income - Year 1974", x = "", y = "")
print(bb)
b2 <- ggplot(data = statedf, aes(state.region, Income))
b2 <- b2 + geom_bin2d(bins = 5) + scale_fill_gradient(low = "orange", high = "darkgreen")
print(b2)
The following 3 plots display the spatial distribution of the geographic center of each State (Alaska and Hawaii are excluded).
m <- ggplot(statedf, aes(x, y))
m + geom_point(color = "darkblue", aes(size = Population)) + geom_density2d(aes(colour = state.region)) + theme_bw()
m + geom_point(aes(colour = state.region, size = Income)) + geom_density2d(size = 1.0) + theme_bw()
m + geom_point() + stat_density2d(aes(fill = ..level..), geom = "polygon")
library(plyr)
usmap <- map_data("state")
names(statedf)[5] <- "region"
statedf$region <- tolower(as.character(statedf$region))
statedf$IncomeLevel <- cut(statedf$Income, breaks = 5, include.lowest = TRUE)
mapdf <- join(x = usmap, y = statedf, by = "region", type = "inner")
statedf[head(order(statedf[, 8], decreasing = TRUE), 15), c(1, 8)]
## state.abb Income
## Connecticut CT 5348
## Maryland MD 5299
## New Jersey NJ 5237
## Nevada NV 5149
## California CA 5114
## Illinois IL 5107
## North Dakota ND 5087
## New York NY 4903
## Colorado CO 4884
## Washington WA 4864
## Florida FL 4815
## Delaware DE 4809
## Massachusetts MA 4755
## Michigan MI 4751
## Virginia VA 4701
mp <- ggplot(data = mapdf, aes(x = long, y = lat))
mp <- mp + geom_polygon(aes(group = group, fill = IncomeLevel), colour = "red")
mp <- mp + scale_fill_brewer(palette = "YlGnBu")
mp <- mp + coord_fixed(ratio = 1.25)
mp <- mp + theme_bw()
mp <- mp + labs(title = "US per capita Income - Year 1974", x = "", y = "", fill = "Income level")
mp