Mahbubul Majumder, PhD
Sep 11, 2014
Data Type | Common Plot Type |
---|---|
Numerical | histogram, dotplot, density plot, scatterplot, boxplot |
Categorical | bar chart, mosaic |
Date | time series |
Mixed | boxplot, parallel cordinate plot |
Spatial | maps, google map, contour plot or 2d density |
library(maps)
library(ggplot2)
map.dat <- map_data("world")
ggplot() + geom_polygon(aes(long,lat, group=group), fill="grey65", data=map.dat) + theme_bw() + theme(axis.text = element_blank(), axis.title=element_blank())
ggplot(map.dat, aes(x=long, y=lat, group=group, fill=region))+ geom_polygon() + theme(legend.position = "none")
us.dat <- map_data("state")
ct.dat <- map_data("county")
ggplot() + geom_polygon(aes(long,lat, group=group), fill="grey65", data=ct.dat) + geom_polygon(aes(long,lat, group=group), color='white', fill=NA, data=us.dat) + theme_bw() + theme(axis.text = element_blank(), axis.title=element_blank())
library(scales) # for function alpha()
us.dat <- map_data("state")
ggplot(us.dat, aes(x=long, y=lat, group=group)) + geom_polygon(fill="grey65", colour = alpha("white", 1/2), size = 0.2) + theme_bw() + theme(legend.position = "none", text = element_blank(), line = element_blank()) + coord_map("polyconic")
pdat <- read.csv("./tempdat.csv")
ggplot(pdat, aes(x=long, y=lat,group=group)) +
geom_polygon(aes(fill=temp), colour = alpha("white", 1/2), size = 0.2) + theme_bw() +
theme(legend.position = "none", text = element_blank(), line = element_blank()) +
scale_fill_continuous(low="blue", high="hotpink")
World petroleum use data, obtained from Nasa website
We need to clean the data before plotting such a map
Yet we see trouble with the data
Map data is obtained from R
package rgdal
ggplot(choropleth, aes(long, lat, group = group)) +
geom_polygon(aes(fill = rate_d), colour = alpha("white", 1/2), size = 0.2) +
geom_polygon(data = state_df, colour = "white", fill = NA) +
scale_fill_brewer(palette = "PuRd")
ggmap
package of R
works with ggplot
## install.packages("ggmap")
library(ggmap)
geocode("University NE, Omaha")
lon lat
1 -96.01 41.26
geocode("PKI, Omaha")
lon lat
1 -96 41.25
geocode("Disney land")
lon lat
1 -117.9 33.81
revgeocode(c(-96,41))
[1] "5615 Nebraska 66, Plattsmouth, NE 68048, USA"
mapdist("Omaha","lincoln")
from to m km miles seconds minutes hours
1 Omaha lincoln 88119 88.12 54.76 3442 57.37 0.9561
? mapdist
crimes <- read.csv("./omaha-crimes.csv")
omaps <- get_map(location = 'Omaha', source = 'stamen', maptype = 'toner')
ggmap(omaps) +
geom_point(size=5, alpha = 1/2, aes(lon,lat, color=type), data=crimes)
get_map(location = 'Omaha')
get_map(location = 'Omaha', maptype = 'satellite')
ggmap(omaps) + stat_bin2d(aes(x = lon, y = lat, colour = type, fill = type), size = .5, bins = 30, alpha = 1/2, data = crimes) + xlim(c(-96.25,-95.85)) + ylim(c(41.15,41.4))
get_map(location = 'Omaha', maptype = 'roadmap', zoom = 11)
ggmap(omapg) + stat_density2d(aes(x = lon, y = lat, fill = ..level.., alpha = ..level..),
size = 2, bins = 4, data = crimes, geom = "polygon")
get_map(location = 'Omaha', maptype = 'roadmap', zoom = 11, color='bw')
ggplot2
web site with nice documentation
http://docs.ggplot2.org/current/
ggmap: Spatial Visualization with ggplot2 by David Kahle and Hadley Wickham
If you want to work with geo-spatial data, keep this ggmap
cheat sheet
https://www.nceas.ucsb.edu/~frazier/RSpatialGuides/ggmap/ggmapCheatsheet.pdf