I am trying to plot some geolocational data pertaining to Great Britain and Ireland in ggplot. Running the following code, I can successfully map some values from this tab-separated file onto the GBR shapefile data found here (country = Great Britain):
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)
Running that code yields:
What I would like to do now is to add data pertaining to Ireland to my plot. I downloaded the "IRL" shapefiles from the same site that provided the GBR shapefiles, but then I ran into a series of roadblocks. I have tried combining IRL_adm1.csv
and GBR_adm2.csv
(renaming the id values in the former to avoid conflicts), but nothing has worked yet. Before hacking the rest of the way to a kludgy solution, I thought I should stop and post the following question on SO: Is there a reasonably straightforward way to combine the GBR and IRL files in a single plot? I would be very grateful for any ideas or suggestions others can offer on this question.
Process. Click on the Vector menu, Data Management tools, Merge Shapefiles to One... Ensure the correct shapefile type is selected (points, lines, or polygons). Select the folder where the two (or more) shapefiles you want to merge are located using the Browse button.
If your Britain and Ireland shapefiles use the same projection/CRS, you can add both layers to a plot without needing to join them like this:
ggplot() +
geom_polygon(data = gbrshapefortified, aes(long, lat, group = group)) +
geom_polygon(data = irlshapefortified, aes(long, lat, group = group)) +
coord_equal()
I.e. you don't need to combine them if you're just plotting layers and the thematic values you're plotting don't depend on each other.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With