6

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:enter image description here

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.

duhaime
  • 25,611
  • 17
  • 169
  • 224
  • my opinion may be biased, but I'd try merging two shape file into one first, before visualizing them, using tool like this http://gis-programming.com/?p=194, or http://gis.stackexchange.com/questions/25061/how-to-merge-multiple-layers-to-one-layer-using-qgis . problem you may see is that you want to use adm2 for gbr and irl has only to adm1, if it had adm2 as well things could be easier... I would try somehow secure id_1 from irl and id_2 from gbr and come up with unified id, then join up with your data in r/ggplot – yosukesabai Nov 29 '14 at 04:49
  • I just tried the method using ogr2ogr. I can get merged shape file, where id_2 has valid value for Britain, and id_2 is null for polygon for Ireland part. so I would then come up with id_duhaime which picks id_1 and id_2 depending on it is Ireland or Britain, while avoiding conflict, and then prepare txt file of yours using your ID. – yosukesabai Nov 29 '14 at 05:03
  • Many thanks for your thoughts, yosukesabai! Check out the simple solution below--it works like a charm – duhaime Nov 29 '14 at 14:41

1 Answers1

6

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.

Phil
  • 4,344
  • 2
  • 23
  • 33