Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. library(rgdal)
  2. library(ggplot2)
  3. library(rgeos)
  4. library(plyr)
  5.  
  6. #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)
  7. shape.dir <- "C:\Users\Douglas\Desktop\estc_clean_analysis\geoanalysis\GBR_adm"
  8.  
  9. #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
  10. uk.shp <- readOGR(shape.dir, layer = "GBR_adm2")
  11.  
  12. #read in csv with values by county
  13. small_geo_data <- read.csv(file = "small_geo_sample.txt", header=TRUE, sep="t", na.string=0, strip.white=TRUE)
  14.  
  15. #fortify prepares the data for ggplot
  16. uk.df <- fortify(uk.shp, region = "ID_2") # convert to data frame for ggplot
  17.  
  18. #now combine the values by id values in both dataframes
  19. combined.df <- join(small_geo_data, uk.df, by="id")
  20.  
  21. #now build plot up layer by layer
  22. ggp <- ggplot(data=combined.df, aes(x=long, y=lat, group=group))
  23. ggp <- ggp + geom_polygon(aes(fill=value)) # draw polygons
  24. ggp <- ggp + geom_path(color="grey", linestyle=2) # draw boundaries
  25. ggp <- ggp + coord_equal()
  26. ggp <- ggp + scale_fill_gradient(low = "#ffffcc", high = "#ff4444",
  27. space = "Lab", na.value = "grey50",
  28. guide = "colourbar")
  29. ggp <- ggp + labs(title="Plotting Values in Great Britain")
  30. # render the map
  31. print(ggp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement