library(install.load) load_package("USGSstates2k", "ie2miscdata", "data.table", "spatstat", "maptools", "ggplot2") # load needed packages using the load_package function from the install.load package (it is assumed that you have already installed these packages) data(weather_results) # load the weather_results data (containing the site information for US weather stations) data(states2k) # load the states2k data (for the US map) weather_results_map <- copy(weather_results) # copy the weather_results using data.table setnames(weather_results_map, 3:4, c("lat", "lon")) # set the names of columns 3 and 4 using data.table # Source 1 - 6 begins # set coordinates and projection class(weather_results_map) coordinates(weather_results_map) <- ~ lon + lat class(weather_results_map) proj4string(weather_results_map) <- CRS("+proj=longlat +datum=NAD83") weather_results_map <- spTransform(weather_results_map, CRS(proj4string(states2k))) weather_results_map <- data.frame(weather_results_map) # Switch to x/y from lat/lon # change variable names names(weather_results_map)[names(weather_results_map) == "lon"] <- "x" names(weather_results_map)[names(weather_results_map) == "lat"] <- "y" # read in shapefile with maptools, preferred for spatstat US <- readShapeSpatial(system.file("shapefiles", "states2k.shp", package = "USGSstates2k")) USpoly <- as(US, "SpatialPolygons") USregions <- slot(USpoly, "polygons") USregions <- lapply(USregions, function(x) { SpatialPolygons(list(x)) }) USwindows <- lapply(USregions, as.owin) USh <- hyperframe(window = USwindows) USh <- cbind.hyperframe(USh, US@data) USowin <- as(USpoly, "owin") # Source 7 # determine which locations are within the borders of the US (including Alaska, Hawai'i, and Puerto Rico) insideUS <- which(inside.owin(weather_results_map$x, weather_results_map$y, USowin)) # Source 7 weather_results_keep <- weather_results[insideUS, ] line1 <- "Design wet bulb temperatures - 1% exceedance values" # create a data.table copy of the sites that are kept weather_results_map_keep <- copy(weather_results_keep) setnames(weather_results_map_keep, 3:4, c("lat", "lon")) # set coordinates and projection for the locations contained within the US class(weather_results_map_keep) coordinates(weather_results_map_keep) <- ~ lon + lat class(weather_results_map_keep) proj4string(weather_results_map_keep) <- CRS("+proj=longlat +datum=NAD83") weather_results_map_keep <- spTransform(weather_results_map_keep, CRS(proj4string(states2k))) weather_results_map_keep <- data.frame(weather_results_map_keep) # Switch to x/y from lat/lon # change variable names names(weather_results_map_keep)[names(weather_results_map_keep) == "lon"] <- "x" names(weather_results_map_keep)[names(weather_results_map_keep) == "lat"] <- "y" # plot the map using ggplot2 colorsnow <- c("Weather Data Sites" = "#3591d1") p <- ggplot() + geom_polygon(data = states2k, aes(x = long, y = lat, group = group), colour = "black", fill = "white") p <- p + geom_point(data = weather_results_map_keep, aes(x = x, y = y, color = "Weather Data Sites")) p <- p + labs(x = "", y = "", title = "US Engineering Weather Sites Map") p <- p + scale_colour_manual(values = colorsnow, labels = c("Weather Data Sites"), name = "Legend") p <- p + theme(axis.ticks.y = element_blank(), axis.text.y = element_blank(), # get rid of x ticks/text axis.ticks.x = element_blank(), axis.text.x = element_blank(), # get rid of y ticks/text plot.title = element_text(lineheight = 0.8, face = "bold", vjust = 1), # make title bold and add space panel.grid.major = element_blank(), # Source 3 / get rid of major grid panel.grid.minor = element_blank()) # Source 3 / get rid of minor grid print(p) # Source 1 - 6 ends
Mapping in R using the ggplot2 package Posted on July 16, 2014 by ZevRoss. See http://zevross.com/blog/2014/07/16/mapping-in-r-using-the-ggplot2-package/.
use proj4 to specify Robinson projection with R ggmap and ggplot2 packages? - Geographic Information Systems Stack Exchange asked by Abe on Dec 19 2012 & answered by radek on Dec 20 2012. See http://gis.stackexchange.com/questions/44387/use-proj4-to-specify-robinson-projection-with-r-ggmap-and-ggplot2-packages.
r - How to increase size of the points in ggplot2, similar to cex in base plots? - Stack Overflow answered by Gavin Simpson on Mar 20 2012. See http://stackoverflow.com/questions/9789613/how-to-increase-size-of-the-points-in-ggplot2-similar-to-cex-in-base-plots.
r - ggplot2: add color to boxplot - "Continuous value supplied to discrete scale" error - Stack Overflow answered by Brian Diggs on May 29 2012. See http://stackoverflow.com/questions/10805643/ggplot2-add-color-to-boxplot-continuous-value-supplied-to-discrete-scale-er.
r - Interval size and colors for points on USA map - discrete error - Stack Overflow answered and edited by zx8754 on Mar 23 2015. See http://stackoverflow.com/questions/29216265/interval-size-and-colors-for-points-on-usa-map-discrete-error.
r - remove grid, background color and top and right borders from ggplot2 - Stack Overflow edited by Sandy Muspratt on Apr 17 2015. See http://stackoverflow.com/questions/10861773/remove-grid-background-color-and-top-and-right-borders-from-ggplot2.
R-sig-geo - Problem in converting SpatialPolygonsDataFrame to owin object by Edzer J. Pebesma on Sep 13, 2006. See http://r-sig-geo.2731867.n2.nabble.com/Problem-in-converting-SpatialPolygonsDataFrame-to-owin-object-td2762586.html.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.