Ggplot2: merge shapefiles from two different geodata

I am trying to build some geolocation data related to the UK and Ireland in ggplot. By running the following code, I can successfully match some values ​​from this file with section separators for the shapefile data of the GBR file found here (country = United Kingdom):

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

Now I would like to add data related to Ireland to my plot. I downloaded the IRL shapefiles from the same site that provided the GBR shapefiles, but then I came across a number of roadblocks. I tried to combine IRL_adm1.csv and GBR_adm2.csv (renaming id values ​​in the first to avoid conflicts), but so far nothing has worked. Before hacking the rest of the way to kludgy's solution, I thought I should stop and post the next question on SO: Is there a simple enough way to combine GBR and IRL files in one plot? I would be very grateful for any ideas or suggestions that others may offer on this subject.

+5
source share
1 answer

If your shaders in the UK and Ireland use the same forecast / CRS, you can add both layers to the plot without having to join them as follows:

 ggplot() + geom_polygon(data = gbrshapefortified, aes(long, lat, group = group)) + geom_polygon(data = irlshapefortified, aes(long, lat, group = group)) + coord_equal() 

those. you do not need to combine them if you just draw layers, and the thematic meanings that you draw are independent of each other.

+4
source

Source: https://habr.com/ru/post/1207921/


All Articles