Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(rgdal)
- library(ggplot2)
- library(rgeos)
- library(plyr)
- #this data comes from http://www.gadm.org/country (download the Great Britain data set, and set path to the downloaded data's topmost directory)
- shape.dir <- "C:\Users\Douglas\Desktop\estc_clean_analysis\geoanalysis\GBR_adm"
- #the first parameter we pass to readOGR species the location of the shapefile we want to read in; layer indicates which shapefile in that dir we want to read in. Data via UK shapefile from http://www.gadm.org/country
- uk.shp <- readOGR(shape.dir, layer = "GBR_adm2")
- #read in csv with values by county
- small_geo_data <- read.csv(file = "small_geo_sample.txt", header=TRUE, sep="t", na.string=0, strip.white=TRUE)
- #fortify prepares the data for ggplot
- uk.df <- fortify(uk.shp, region = "ID_2") # convert to data frame for ggplot
- #now combine the values by id values in both dataframes
- combined.df <- join(small_geo_data, uk.df, by="id")
- #now build plot up layer by layer
- ggp <- ggplot(data=combined.df, aes(x=long, y=lat, group=group))
- ggp <- ggp + geom_polygon(aes(fill=value)) # draw polygons
- ggp <- ggp + geom_path(color="grey", linestyle=2) # draw boundaries
- ggp <- ggp + coord_equal()
- ggp <- ggp + scale_fill_gradient(low = "#ffffcc", high = "#ff4444",
- space = "Lab", na.value = "grey50",
- guide = "colourbar")
- ggp <- ggp + labs(title="Plotting Values in Great Britain")
- # render the map
- print(ggp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement