load packages and set working directory
library(ggplot2)
library(raster)
## Loading required package: sp
library(ggplot2)
library(ggsn)
## Loading required package: grid
##
## Attaching package: 'ggsn'
## The following object is masked from 'package:raster':
##
## scalebar
# set factors to false
options(stringsAsFactors = FALSE)
setwd("C:/Users/Public/maps/")
Load base maps. Maps were downloaded from UCDavis GADM database. ph0 is the outline of the country. ph1 shows municipal boundaries. ph2 shows barangay boundaries
ph0<-readRDS("C:/Users/Public/maps/gadm36_PHL_0_sp.rds") # national level
ph2<-readRDS("C:/Users/Public/maps/gadm36_PHL_2_sp.rds") # municipality level
ph3<-readRDS("C:/Users/Public/maps/gadm36_PHL_3_sp.rds") # barangay level
View map data. Check what information is included.
ph0list<-ph0@data
ph2list<-ph2@data
ph3list<-ph3@data
In this case, we will make a map of a specific municipality (Lobo, Batangas) including its barangays. We will use the ph2 map. All municipalities are listed under the column “Name_2”. All barangays are listed under the column “NAME_3”. We will make a subset of Lobo.
Lobo<-(ph3[ph3$NAME_2 %in% "Lobo",])
Load coordinates of our survey sites. this is a csv file with latitude and longitude of each site.
lobosites<-read.csv("C:/Users/Public/maps/samplesites.csv", header = T)
attach(lobosites)
lobosites
make map of Lobo
library(ggplot2)
lobomap<-ggplot()+
geom_polygon(data=Lobo, aes(long,lat, group=group), colour="grey10",fill="darkseagreen3")+
geom_point(data = lobosites, aes(x = X, y = Y), size = 3,
shape = 21, fill = "darkred") + # this will add red points to your survey sites
geom_text(data = lobosites, aes(X-0.01, Y-0.004, label=sitename), size = 3, vjust = 0.8)+ #to adjust position of site names, modify X and Y
coord_equal()+
theme_bw()+
xlab("")+
ylab("")+
theme(axis.text.y =element_text(angle = 90, hjust=0.5, size = 8),
axis.text.x =element_text(size = 8))
## Regions defined for each Polygons
If we want to zoom in to the map to show the coast and sample sites clearly we can play around the following code.
lobomap<-lobomap + coord_quickmap(xlim=c(121.18, 121.3), ylim=c(13.58,13.67))
## Coordinate system already present. Adding new coordinate system, which will replace the existing one.
lobomap
add scale bar and northing to map
lobomap2<-lobomap+
ggsn::scalebar(x.min=121.18, x.max=121.3, y.min=13.58, y.max= 13.67, #specify based on minimum and maximum coordinates of map
model = "WGS84", dist_unit = "km", transform = TRUE,
location = "bottomleft",
height = 0.015, dist = 1, st.size = 2, border.size = 0.4) +
ggsn::north(x.min=121.18, x.max=121.3, y.min=13.58, y.max= 13.67, location = "bottomleft", scale = 0.15, symbol = 10) #this will add northing to your map
lobomap2
make inset country map
#location of rectangle in inset based on coordinates
insetlocation<-data.frame(xmin=121.10,xmax=121.375,ymin=13.55 ,ymax=13.8)
philinset<-ggplot()+geom_polygon(data=ph0, aes(long,lat,group=group),colour="grey10",fill="grey66")+
coord_equal()+theme_bw()+labs(x=NULL,y=NULL)+
annotate("text", label = "Verde Island Passage", size = 2, colour = "red")+
scale_x_continuous(breaks=seq(117.5,125, 2.5), labels=c(paste(seq(117.5,125, 2.5),"°E", sep="")))+
scale_y_continuous(breaks=seq(5,20, 5), labels=c(paste(seq(5,20, 5),"°N", sep="")))+
geom_rect(data = insetlocation, aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax), alpha=0, colour="red", size = 1, 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(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
## Regions defined for each Polygons
philinset
combine map, figure saved in working directory
lobomap2_inset<-lobomap2 + annotation_custom(ggplotGrob(philinset),
xmin = 121.275, ymin = 13.625,
xmax = 121.3)
lobomap2_inset
ggsave("lobomap2_inset.png")
## Saving 7 x 5 in image