GGPLOT in class exercise


library(ggthemes)
## Error: there is no package called 'ggthemes'
library(ggplot2)
library(maptools)
## Warning: package 'maptools' was built under R version 2.15.3
## Warning: package 'sp' was built under R version 2.15.3
library(hexbin)
## Warning: package 'hexbin' was built under R version 2.15.3
library(classInt)
## Warning: package 'classInt' was built under R version 2.15.3
## Warning: package 'e1071' was built under R version 2.15.3
library(RColorBrewer)
usa = readShapePoly("H:/Geog5023/Maps and Multiple Regression/USA.shp")
usa = usa[, c(1:8, 14:30)]
usa = na.omit(usa)
plot1 = ggplot(data = usa@data, aes(x = Obese, y = homevalu))
plot1 + geom_point()

plot of chunk unnamed-chunk-1

plot1 + geom_point() + scale_x_log10() + scale_y_log10()

plot of chunk unnamed-chunk-1

plot1 + geom_point(alpha = 0.1) + scale_x_log10() + scale_y_log10()

plot of chunk unnamed-chunk-1

plot1 + geom_point(alpha = 0.1) + geom_smooth(method = "lm")

plot of chunk unnamed-chunk-1

plot1 + geom_point(alpha = 0.1) + geom_smooth(method = "loess")

plot of chunk unnamed-chunk-1

plot1 + stat_binhex()

plot of chunk unnamed-chunk-1

plot1 + geom_bin2d()

plot of chunk unnamed-chunk-1

plot1 + geom_density2d()

plot of chunk unnamed-chunk-1


usa$good_states = ifelse(usa$STATE_NAME %in% c("Utah", "California", "Colorado", 
    "Wyoming"), "it good", "it ok")
usa$good_states = as.factor(usa$good_states)

plot2 = ggplot(data = usa@data, aes(x = Obese, y = homevalu, color = good_states))
plot2 + geom_point()

plot of chunk unnamed-chunk-1

plot2 = ggplot(data = usa@data, aes(x = Obese, y = homevalu, color = good_states, 
    shape = good_states))
plot2 + stat_smooth()

plot of chunk unnamed-chunk-1

## geom_smooth: method='auto' and size of largest group is >=1000, so
## using gam with formula: y ~ s(x, bs = 'cs'). Use 'method = x' to change
## the smoothing method.
plot2 + geom_point() + stat_smooth(method = "lm", se = T, lsd = 0.5, lty = 1)

plot of chunk unnamed-chunk-1

# lwd controls line thickenss lty controls line type 1= solid line, higher
# numbers various forms of dashed lines. se can be used ti turn off the
# grey standaed error envelopes.
plot3 = ggplot(data = usa@data, aes(x = pctcoled, y = pcincome))
plot3 + geom_point() + xlab("Per Capita Income") + ylab("Pecent College Educated") + 
    ggtitle("US Counties (2000)\nPercent College Educated by Per Capita Income")

plot of chunk unnamed-chunk-1

plot4 = ggplot(data = usa@data, aes(x = pctcoled, y = pcincome, color = unemploy)) + 
    geom_point() + ylab("Per Capita Income") + xlab("Percent College Educated") + 
    ggtitle("US Counties (2000)\nPercent College Educated by Per Capita Income") + 
    scale_color_gradient2("Unemployment", breaks = c(min(usa$unemploy), mean(usa$unemploy), 
        max(usa$unemploy)), labels = c("Below Average", "Average", "Above Average"), 
        low = "green", mid = "yellow", high = "red", midpoint = mean(usa$unemploy))
plot4

plot of chunk unnamed-chunk-1

plot4 + facet_grid(. ~ good_states)

plot of chunk unnamed-chunk-1

# If you wanted to go crazy you could do: plot4 + facet_grid(.~
# STATE_NAME)
plot4 + theme_classic()

plot of chunk unnamed-chunk-1

plot4 + theme_economist()
## Error: could not find function "theme_economist"
plot4 + theme_solarized()
## Error: could not find function "theme_solarized"
plot4 + theme_tufte()
## Error: could not find function "theme_tufte"
sethTheme = theme(panel.background = element_rect(fill = "black"), plot.background = element_rect(fill = "black"), 
    panel.grid.minor = element_blank(), panel.grid.major = element_line(linetype = 3, 
        colour = "white"), axis.text.x = element_text(colour = "grey80"), axis.text.y = element_text(colour = "grey80"), 
    axis.title.x = element_text(colour = "grey80"), axis.title.y = element_text(colour = "grey80"), 
    legend.key = element_rect(fill = "black"), legend.text = element_text(colour = "white"), 
    legend.title = element_text(colour = "black"), legend.background = element_rect(fill = "black"), 
    axis.ticks = element_blank())
plot4 + sethTheme

plot of chunk unnamed-chunk-1


# Making Maps
usa_geom = fortify(usa, region = "FIPS")
## Error: isTRUE(gpclibPermitStatus()) is not TRUE
usa_map_df = merge(usa_geom, usa, by.x = "id", by.y = "FIPS")
## Error: object 'usa_geom' not found
map1 = ggplot(usa_map_df, aes(long, lat, group = group)) + geom_polygon(data = usa_map_df, 
    aes(fill = Bush_pct)) + coord_equal() + scale_fill_gradient(low = "yellow", 
    high = "red") + geom_path(data = usa_geom, aes(long, lat, group = group), 
    lty = 3, lwd = 0.1, color = "white")
## Error: object 'usa_map_df' not found
map1
## Error: object 'map1' not found
map1 + sethTheme
## Error: object 'map1' not found

ci = classIntervals(usa$Bush_pct, n = 5, style = "quantile")
labels = c("[0 - 50%]", "[50% - 58%]", "[58% - 64%]", "[64% - 71%]", "[71% - 93%]")
usa_map_df$bushBreaks = cut(usa_map_df$Bush_pct, breaks = ci$brks, labels = labels)
## Error: object 'usa_map_df' not found
map2 = ggplot(aes(long, lat, group = group), data = usa_map_df) + geom_polygon(data = usa_map_df, 
    aes(fill = bushBreaks)) + coord_equal()
## Error: object 'usa_map_df' not found
map2
## Error: object 'map2' not found
map2 + scale_fill_brewer("Votes for Bush in 2004 (%)", palette = "YlGnBu") + 
    sethTheme + ggtitle("votes for Bush in 2004 (%)") + theme(plot.title = element_text(size = 24, 
    face = "bold", color = "white", hjust = 2))
## Error: object 'map2' not found