Trying to get objects that have maps assigned to them to accept certain formatting for inset map

Here is some code that works although the data files have not been provided here. I have adapted some code that I found on line the British Columbia maps (Health Service Delivery area). The maps work fine outside of the p1 and p2 objects but the p1 (for main map) and p2 (for inset map) objects seem to ignore the formatting for the quintile cuts, data labels and zoom.plot when I try to include that code in the p1 and p2 objects. In this code here I have left the p1 and p2 objects with that extra formatting out so that they work but I would like to be able to get the p1 and p2 objects to accept the extra formatting so that I can place the zoomed version of the BC map into the extra grid. I am new to R (a novice ). The code above the p1 and p2 assignments works to generate the maps as desired but it fails within the p1 and p1 assignments. When I use "plot" instead of "ggplot", the same thing happens...

ANY HELP IS APPRECIATED ...

Richard


################################## 
# Creating Inset Maps in ggplot2 #
################################## 

library(SpatialEpi)
library(rgdal)
library(dplyr)
library(GISTools)
library(haven)
library(maps) 
library(maptools) 
library(rgeos)
library(foreign)
library(boxr)

library(ggplot2) 
library(raster) 
library(grid)
library(gridExtra)
library(zoom)

setwd("X:/R//WORK_1_Test_R/TEST_FOLDER")


#Read data file for the main map
Ind.data1 <- read_sas("R:/R/Data/CCHS HSDA Data/current_smokers.sas7bdat")

# Choose variables from data file to join to the shape file
Ind.data <- subset(Ind.data1,Smoking_Status==1, select=c(HSDA_NUM, Frequency, WgtFreq, Percent))


#Read shape file into R
mapMain <- readOGR (dsn="H:/R/Shape Files/Boundaries_-_Health_Service_Delivery_Areas/HSDA_2013.shp", layer="HSDA_2013")



#base plot Main
ggplot(mapMain,  border="gray", axes=TRUE)

# join the spatial and non-spatial files by HSDA_NUM
mapMain@data<-left_join(mapMain@data,Ind.data,by="HSDA_NUM")

# Assign the quantile cuts (quiintiles in this case) 
quantileCuts(mapMain@data$Percent, n=5)

#apply shading based on regional data values
choropleth(mapMain,Ind.data$Percent, shading=shades)

# Add data labels to regions
pointLabel (coordinates(mapMain), labels=as.character(mapMain$HSDA_NUM), cex=0.6, col=2, jiggle=1.11,
            offset=1.1, allowSmallOverlap = FALSE)

# add a north-point arrow
north.arrow(410000, 1380000,10000,cex.lab=0.8, col='cyan')

#put a box around the entire map
box(which = "plot", lty = "solid")


# Object for main map 
  p1 <- ggplot(mapMain, border="grey", axes=TRUE)+geom_polygon(data=mapMain, aes(long,lat,group=group),colour="grey10",fill="#fff7bc")+
  coord_equal()+theme_bw()+labs(x=NULL,y=NULL)+
  scale_x_continuous(breaks=seq(113.8, 138.9, 1.0), labels=c(paste(seq(113.8, 138.9, 1.0),"°W", sep="")))+  
  scale_y_continuous(breaks=seq(47.2, 60.1, 1.0), labels=c(paste(seq(47.2, 60.1, 1.0),"°N", sep="")))+ 
  theme(axis.text.y =element_text(angle = 90, hjust=0.5))


# Extent rectangle for inset map 
pol<-data.frame(xmin=122.1, xmax=123.3 ,ymin=49.03 ,ymax=49.3) 



#base plot Main
ggplot(mapInset,  border="gray", axes=TRUE)

# join the spatial and non-spatial files by HSDA_NUM
mapInset@data<-left_join(mapInset@data,Ind.data,by="HSDA_NUM")

# Assign the quantile cuts (quiintiles in this case) 
quantileCuts(mapInset@data$Percent, n=5)

#apply shading based on regional data values
choropleth(mapInset,Ind.data$Percent, shading=shades)

# Add data labels to regions
pointLabel (coordinates(mapInset), labels=as.character(mapInset$HSDA_NUM), cex=0.6, col=2, jiggle=1.11,
            offset=1.1, allowSmallOverlap = FALSE)

# zoom the desired area for the inset map
zoomplot.zoom(xlim=c(1030000, 1300000), ylim=c(370000,547000))


# Object for inset map 
p2 <- ggplot(mapInset)+geom_polygon(data=mapInset, aes(long,lat,group=group),colour="grey10",fill="#fff7bc")+ 
  coord_equal()+theme_bw()+labs(x=NULL,y=NULL)+ 
  scale_x_continuous(breaks=seq(122.1, 123.3, 0.1), labels=c(paste(seq(122.1, 123.3, 0.1),"°W", sep="")))+ 
  scale_y_continuous(breaks=seq(49.0, 49.3, 0.1), labels=c(paste(seq(49.0, 49.3, 0.1),"°N", sep="")))+ 
  geom_rect(data = pol, aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax), alpha=0, colour="black", size = 0.5, linetype=1)+ 
  theme(axis.text.x =element_blank(),axis.text.y= element_blank(), axis.ticks=element_blank(),axis.title.x =element_blank(), 
        axis.title.y= element_blank()) 


#set output file name and directory
png(file="X:/R/Data/CCHS HSDA Maps/smoking.png",w=1800,h=1800, res=300)


 grid.newpage() 
 v1<-viewport(width = 1, height = 1, x = 0.5, y = 0.5) #plot area for the main map 
 v2<-viewport(width = 0.3, height = 0.3, x = 0.18, y = 0.21) #plot area for the inset map
 print(p1,vp=v1)  
 print(p2,vp=v2) 
 dev.off()

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.