This package was developed to simplify the production of maps in the CAMLR Convention Area. It provides two main categories of functions: load functions and create functions. Load functions are used to import spatial layers from the online CCAMLR GIS such as the ASD boundaries. Create functions are used to create layers from user data such as polygons and grids.
Due to the retirement of some packages that the CCAMLRGIS package use to rely on, since CCAMLRGIS V4.0.0 the package relies on the sf package, users may need to familiarize themselves with it. For those that were using older versions, the main difference is in plotting commands.
Plotting a spatial object MyObject used to be:
plot(MyObject)
Since V4, it will be:
plot(st_geometry(MyObject))
Also, to access the data inside spatial objects, instead of MyObject@data, type MyObject directly. You can revert to sp objects with as_Spatial(MyObject) if preferred.
Using sf objects has advantages such as the ability to use Tidyverse methods. Further, additional plotting methods are available, some of which are described in section 5.5. Using sf.
You can install the CCAMLRGIS R package from CRAN with:
install.packages("CCAMLRGIS")
First, install the package by typing:
install.packages("CCAMLRGIS")
Then, load the package by typing:
library(CCAMLRGIS)
In order to plot bathymetry data, you will also need to load terra:
library(terra)
All spatial manipulations are made using the South Pole Lambert Azimuthal Equal Area projection (type ?CCAMLRp for more details).
#Map with axes, to illustrate projection
#Set the figure margins as c(bottom, left, top, right)
par(mai=c(1.2,0.7,0.5,0.45),xpd=TRUE)
#plot entire Coastline
plot(st_geometry(Coast[Coast$ID=='All',]),col='grey',lwd=0.1)
#Add reference grid
add_RefGrid(bb=st_bbox(Coast[Coast$ID=='All',]),ResLat=10,ResLon=20,LabLon=-40,fontsize=0.8,lwd=0.5)
#add axes and labels
axis(1,pos=0,at=seq(-4000000,4000000,by=1000000),tcl=-0.15,labels=F,lwd=0.8,lwd.ticks=0.8,col='blue')
axis(2,pos=0,at=seq(-4000000,4000000,by=1000000),tcl=-0.15,labels=F,lwd=0.8,lwd.ticks=0.8,col='blue')
text(seq(1000000,4000000,by=1000000),0,seq(1,4,by=1),cex=0.75,col='blue',adj=c(0.5,1.75))
text(seq(-4000000,-1000000,by=1000000),0,seq(-4,-1,by=1),cex=0.75,col='blue',adj=c(0.5,1.75))
text(0,seq(1000000,4000000,by=1000000),seq(1,4,by=1),cex=0.75,col='blue',adj=c(1.75,0.5))
text(0,seq(-4000000,-1000000,by=1000000),seq(-4,-1,by=1),cex=0.75,col='blue',adj=c(1.75,0.5))
text(0,0,0,cex=0.75,col='blue',adj=c(-0.5,-0.5))
text(5200000,0,expression('x ('*10^6~'m)'),cex=0.75,col='blue')
text(0,4700000,expression('y ('*10^6~'m)'),cex=0.75,col='blue')
Prior to detailing the package’s capabilities, a set of basic commands are shown here to display a few core mapping elements. All scripts use the low-resolution bathymetry raster included in the package (‘SmallBathy’). In order to obtain higher resolution bathymetry data, use the Load_Bathy() function:
#Load_Bathy() example:
=load_Bathy(LocalFile = FALSE,Res=5000)
Bathyplot(Bathy, breaks=Depth_cuts,col=Depth_cols,axes=FALSE,legend=FALSE,mar=c(0,0,0,0))
#Please refer to ?load_Bathy for more details, including how to save the bathymetry data so that you
#do not have to re-download it every time you need it.
#Load ASDs and EEZs
=load_ASDs()
ASDs=load_EEZs()
EEZs#Set the figure margins as c(bottom, left, top, right)
par(mai=c(0,0.4,0,0))
#Plot the bathymetry
plot(SmallBathy,breaks=Depth_cuts,col=Depth_cols,legend=F,axes=F,box=F,mar=c(0,0.4,0,0))
#Add reference grid
add_RefGrid(bb=st_bbox(SmallBathy),ResLat=10,ResLon=20,LabLon=0,fontsize=0.75,lwd=0.75,offset = 4)
#Add color scale
add_Cscale(height=90,fontsize=0.75,offset=-500,width=15,maxVal=-1,lwd=0.5)
#Add ASD and EEZ boundaries
plot(st_geometry(ASDs),add=T,lwd=0.75,border='red')
plot(st_geometry(EEZs),add=T,lwd=0.75,border='red')
#Add coastline (for all ASDs)
plot(st_geometry(Coast[Coast$ID=='All',]),col='grey',lwd=0.01,add=T)
#Add ASD labels
add_labels(mode='auto',layer='ASDs',fontsize=0.6,col='red')
#Load ASDs
=load_ASDs()
ASDs#Subsample ASDs to only keep Subarea 48.6
=ASDs[ASDs$GAR_Short_Label=='486',]
S486#Crop bathymetry to match the extent of S486
=crop(rast(SmallBathy),ext(S486))
B486#Plot the bathymetry
plot(B486,breaks=Depth_cuts,col=Depth_cols,legend=F,axes=F,mar=c(1.4,2,1.4,7))
#Add color scale
add_Cscale(height=80,fontsize=0.7,offset=300,width=15,lwd=0.5,maxVal=-1)
#Add coastline (for Subarea 48.6 only)
plot(Coast[Coast$ID=='48.6',],col='grey',lwd=0.01,add=T)
#Add reference grid
add_RefGrid(bb=st_bbox(B486),ResLat=5,ResLon=10,fontsize=0.75,lwd=0.75,offset = 100000)
#Add Subarea 48.6 boundaries
plot(st_geometry(S486),add=T,lwd=1,border='red')
#Add a -2000m contour
contour(B486,levels=-2000,add=T,lwd=0.5,labcex=0.3)
#Add single label at the centre of the polygon (see ?Labels)
text(Labels$x[Labels$t=='48.6'],Labels$y[Labels$t=='48.6'],labels='48.6',col='red',cex=1.5)
These functions are used to transform user data into spatial layers with the appropriate projection. User data should be provided as a dataframe containing Latitudes and Longitudes in decimal degrees. Depending on the function used, some other variables may be required (see help).
For details, type:
?create_Points
#Prepare layout for 4 sub-plots
par(mfrow=c(2,2),mai=c(0,0.01,0.2,0.01))
#Example 1: Simple points with labels
=create_Points(PointData)
MyPointsplot(st_geometry(MyPoints),main='Example 1',cex.main=0.75,cex=0.5,lwd=0.5)
text(MyPoints$x,MyPoints$y,MyPoints$name,adj=c(0.5,-0.5),xpd=T,cex=0.75)
box()
#Example 2: Simple points with labels, highlighting one group of points with the same name
=create_Points(PointData)
MyPointsplot(st_geometry(MyPoints),main='Example 2',cex.main=0.75,cex=0.5,lwd=0.5)
text(MyPoints$x,MyPoints$y,MyPoints$name,adj=c(0.5,-0.5),xpd=T,cex=0.75)
plot(st_geometry(MyPoints[MyPoints$name=='four',]),bg='red',pch=21,cex=1,add=T)
box()
#Example 3: Buffered points with radius proportional to catch
=create_Points(PointData,Buffer=1*PointData$Catch)
MyPointsplot(st_geometry(MyPoints),col='green',main='Example 3',cex.main=0.75,cex=0.5,lwd=0.5)
text(MyPoints$x,MyPoints$y,MyPoints$name,adj=c(0.5,0.5),xpd=T,cex=0.75)
box()
#Example 4: Buffered points with radius proportional to catch and clipped to the Coast
=create_Points(PointData,Buffer=2*PointData$Catch,Clip=T)
MyPointsplot(st_geometry(MyPoints),col='cyan',main='Example 4',cex.main=0.75,cex=0.75,lwd=0.5)
plot(st_geometry(Coast[Coast$ID=='All',]),add=T,col='grey',lwd=0.5)
box()
For details, type:
?create_Lines
#If your data contains line end locations in separate columns, you may reformat it as follows:
#Original data:
=data.frame(
MyDataLine=c(1,2),
Lat_Start=c(-60,-65),
Lon_Start=c(-10,5),
Lat_End=c(-61,-66),
Lon_End=c(-2,2)
)
#Reformat data to use as input in create_Lines as:
=data.frame(
InputLine=c(MyData$Line,MyData$Line),
Lat=c(MyData$Lat_Start,MyData$Lat_End),
Lon=c(MyData$Lon_Start,MyData$Lon_End)
)
#Prepare layout for 3 sub-plots
par(mai=c(0,0.01,0.2,0.01),mfrow=c(1,3))
#Example 1: Simple and non-densified lines
=create_Lines(LineData)
MyLinesplot(st_geometry(MyLines),col=rainbow(nrow(MyLines)),main='Example 1',cex.main=0.75,lwd=2)
box()
#Example 2: Simple and densified lines (note the curvature of the purple line)
=create_Lines(LineData,Densify=T)
MyLinesplot(st_geometry(MyLines),col=rainbow(nrow(MyLines)),main='Example 2',cex.main=0.75,lwd=2)
box()
#Example 3: Densified, buffered and clipped lines
=create_Lines(LineData,Densify=T,Buffer=c(10,40,50,80,100),Clip=T)
MyLinesplot(st_geometry(MyLines[5:1,]),col=rainbow(nrow(MyLines)),main='Example 3',cex.main=0.75,lwd=1)
plot(Coast[Coast$ID=='All',],col='grey',add=T,lwd=0.5)
box()
Adding a buffer with the argument SeparateBuf set to FALSE results in a single polygon which may be viewed as a footprint:
#Set the figure margins as c(bottom, left, top, right)
par(mai=c(0.01,0.01,0.01,0.01))
#Buffer merged lines
=create_Lines(LineData,Buffer=10,SeparateBuf=F)
MyLines#The resulting polygon has an area of:
$Buffered_AreaKm2
MyLines#> [1] 222654.8
plot(st_geometry(MyLines),col='green',lwd=1)
box()
For details, type:
?create_Polys
#Prepare layout for 3 sub-plots
par(mfrow=c(1,3),mai=c(0,0.01,0.2,0.01))
#Example 1: Simple and non-densified polygons
=create_Polys(PolyData,Densify=F)
MyPolysplot(st_geometry(MyPolys),col='blue',main='Example 1',cex.main=0.75,lwd=0.5)
text(MyPolys$Labx,MyPolys$Laby,MyPolys$ID,col='white',cex=0.75)
box()
#Example 2: Simple and densified polygons (note the curvature of iso-latitude lines)
=create_Polys(PolyData)
MyPolysplot(st_geometry(MyPolys),col='red',main='Example 2',cex.main=0.75,lwd=0.5)
text(MyPolys$Labx,MyPolys$Laby,MyPolys$ID,col='white',cex=0.75)
box()
#Example 3: Buffered and clipped polygons
=create_Polys(PolyData,Buffer=c(10,-15,120))
MyPolysBefore=create_Polys(PolyData,Buffer=c(10,-15,120),Clip=T)
MyPolysAfterplot(st_geometry(MyPolysBefore),col='green',main='Example 3',cex.main=0.75,lwd=0.5)
plot(st_geometry(Coast[Coast$ID=='All',]),add=T,lwd=0.5)
plot(st_geometry(MyPolysAfter),col='orange',add=T,lwd=0.5)
text(MyPolysAfter$Labx,MyPolysAfter$Laby,MyPolysAfter$ID,col='white',cex=0.75)
box()
#Convention area
#The locations of vertices are given clockwise, starting from the northwest corner of 48.3
=data.frame(
CAName="CA",
Lat=c(-50,-50,-45,-45,-55,-55,-60,-60),
Lon=c(-50,30,30,80,80,150,150,-50)
)
#Prepare layout for 2 sub-plots
par(mfrow=c(1,2),mai=c(0,0,0.2,0))
#Example 4: Convention area contour
=create_Polys(CA)
MyPolyplot(st_geometry(MyPoly),col='blue',border='green',main='Example 4',cex.main=0.75,lwd=2)
box()
#Example 5: Convention area contour, coastline clipped
=create_Polys(CA,Clip = TRUE)
MyPolyplot(st_geometry(MyPoly),col='blue',border='green',main='Example 5',cex.main=0.75,lwd=2)
box()
For details, type:
?create_PolyGrids
#Prepare layout for 3 sub-plots
par(mfrow=c(1,3),mai=c(0,0.01,0.2,0.01))
#Example 1: Simple grid, using automatic colors
=create_PolyGrids(GridData,dlon=2,dlat=1)
MyGridplot(st_geometry(MyGrid),col=MyGrid$Col_Catch_sum,main='Example 1',cex.main=0.75,lwd=0.1)
box()
#Example 2: Equal area grid, using automatic colors
=create_PolyGrids(GridData,Area=10000)
MyGridplot(st_geometry(MyGrid),col=MyGrid$Col_Catch_sum,main='Example 2',cex.main=0.75,lwd=0.1)
box()
#Example 3: Equal area grid, using custom cuts and colors
=create_PolyGrids(GridData,Area=10000,cuts=c(0,50,100,500,2000,3500),cols=c('blue','red'))
MyGridplot(st_geometry(MyGrid),col=MyGrid$Col_Catch_sum,main='Example 3',cex.main=0.75,lwd=0.1)
box()
Customizing a grid and adding a color scale:
#Prepare layout for 2 sub-plots
par(mfrow=c(2,1),mai=c(0.2,0.05,0.1,1.3))
#Step 1: Generate your grid
=create_PolyGrids(GridData,Area=10000)
MyGrid
#Step 2: Inspect your gridded data (e.g. sum of Catch) to determine whether irregular cuts are required
hist(MyGrid$Catch_sum,100,cex=0.75,main='Frequency distribution of data',
cex.main=0.5,col='grey',axes=F)
axis(1,pos=0,tcl=-0.15,lwd=0.8,lwd.ticks=0.8,labels=F)
text(seq(0,2500,by=500),-1.5,seq(0,2500,by=500),cex=0.75,xpd=T)
#In this case (heterogeneously distributed data) irregular cuts would be preferable
#Such as:
=c(0,50,100,500,2000,2500)
MyCutsabline(v=MyCuts,col='green',lwd=0.1,lty=2) #Add classes to histogram as green dashed lines
#Step 3: Generate colors according to the desired classes (MyCuts)
=add_col(MyGrid$Catch_sum,cuts=MyCuts,cols=c('yellow','purple'))
Gridcol
#Step 4: Plot result and add color scale
#Use the colors generated by add_col
plot(st_geometry(MyGrid),col=Gridcol$varcol,lwd=0.1)
#Add color scale using cuts and cols generated by add_col
add_Cscale(title='Sum of Catch (t)',cuts=Gridcol$cuts,cols=Gridcol$cols,width=18,
fontsize=0.6,lwd=0.5,height = 100)
box()
This function was designed to create random point locations inside a polygon and within bathymetry strata constraints. A distance constraint between stations may also be used if desired. The examples below use the ‘SmallBathy’ data for illustrative purposes; users should use a higher resolution bathymetry dataset instead, as obtained via the load_Bathy() function.
For details, type:
?create_Stations
First, create a polygon within which stations will be created:
#Create polygons
=create_Polys(
MyPolydata.frame(Name="mypol",
Latitude=c(-75,-75,-70,-70),
Longitude=c(-170,-180,-180,-170))
Densify=T)
,
#Set the figure margins as c(bottom, left, top, right)
par(mai=c(0,0,0,0))
plot(st_geometry(Coast[Coast$ID=='88.1',]),col='grey')
plot(st_geometry(MyPoly),col='green',add=T)
text(MyPoly$Labx,MyPoly$Laby,MyPoly$ID)
box()
Example 1. Set numbers of stations, no distance constraint:
#optional: crop your bathymetry raster to match the extent of your polygon
=crop(rast(SmallBathy),ext(MyPoly))
BathyCroped
#Create stations
=create_Stations(MyPoly,BathyCroped,Depths=c(-2000,-1500,-1000,-550),N=c(20,15,10))
MyStations
#add custom colors to the bathymetry to indicate the strata of interest
=add_col(var=c(-10000,10000),cuts=c(-2000,-1500,-1000,-550),cols=c('blue','cyan'))
MyColsplot(BathyCroped,breaks=MyCols$cuts,col=MyCols$cols,legend=F,axes=F,main="Example 1")
add_Cscale(height=90,fontsize=0.75,width=16,lwd=0.5,offset=-130,cuts=MyCols$cuts,cols=MyCols$cols)
plot(st_geometry(MyPoly),add=T,border='red',lwd=2,xpd=T)
plot(st_geometry(MyStations),add=T,col='orange',cex=0.75,lwd=1.5,pch=3)
Example 2. Set numbers of stations, with distance constraint:
#Create Stations
=create_Stations(MyPoly,BathyCroped,
MyStationsDepths=c(-2000,-1500,-1000,-550),N=c(20,15,10),dist=10)
#add custom colors to the bathymetry to indicate the strata of interest
=add_col(var=c(-10000,10000),cuts=c(-2000,-1500,-1000,-550),cols=c('blue','cyan'))
MyColsplot(BathyCroped,breaks=MyCols$cuts,col=MyCols$cols,legend=F,axes=F,main="Example 2")
add_Cscale(height=90,fontsize=0.75,width=16,lwd=0.5,offset=-130,cuts=MyCols$cuts,cols=MyCols$cols)
plot(st_geometry(MyPoly),add=T,border='red',lwd=2,xpd=T)
plot(st_geometry(MyStations[MyStations$Stratum=='1000-550',]),pch=21,bg='yellow',add=T,cex=0.75,lwd=0.1)
plot(st_geometry(MyStations[MyStations$Stratum=='1500-1000',]),pch=21,bg='orange',add=T,cex=0.75,lwd=0.1)
plot(st_geometry(MyStations[MyStations$Stratum=='2000-1500',]),pch=21,bg='red',add=T,cex=0.75,lwd=0.1)
Example 3. Automatic numbers of stations, with distance constraint:
#Create Stations
=create_Stations(MyPoly,BathyCroped,Depths=c(-2000,-1500,-1000,-550),Nauto=30,dist=10)
MyStations
#add custom colors to the bathymetry to indicate the strata of interest
=add_col(var=c(-10000,10000),cuts=c(-2000,-1500,-1000,-550),cols=c('blue','cyan'))
MyColsplot(BathyCroped,breaks=MyCols$cuts,col=MyCols$cols,legend=F,axes=F,main="Example 3")
add_Cscale(height=90,fontsize=0.75,width=16,lwd=0.5,offset=-130,cuts=MyCols$cuts,cols=MyCols$cols)
plot(st_geometry(MyPoly),add=T,border='red',lwd=2,xpd=T)
plot(st_geometry(MyStations[MyStations$Stratum=='1000-550',]),pch=21,bg='yellow',add=T,cex=0.75,lwd=0.1)
plot(st_geometry(MyStations[MyStations$Stratum=='1500-1000',]),pch=21,bg='orange',add=T,cex=0.75,lwd=0.1)
plot(st_geometry(MyStations[MyStations$Stratum=='2000-1500',]),pch=21,bg='red',add=T,cex=0.75,lwd=0.1)
The function create_Pies() generates pie charts that can be overlaid on maps. The Input data must be a dataframe with, at least, columns for latitude, longitude, class and value. For each location, a pie is created with pie pieces for each class, and the size of each pie piece depends on the proportion of each class (the value of each class divided by the sum of values). Optionally, the area of each pie can be proportional to a chosen variable (if that variable is different than the value mentioned above, the Input data must have a fifth column and that variable must be unique to each location). If the Input data contains locations that are too close together, the data can be gridded by setting GridKm (see Examples 6-8). Once pie charts have been created, the function add_PieLegend() may be used to add a legend to the figure.
For details, type:
?create_Pies
?add_PieLegend#The examples below use the following example datasets:
View(PieData)
View(PieData2)
Example 1. Pies of constant size, all classes displayed:
#Plot the bathymetry (See section 'Local map' where B486 was created)
plot(B486,breaks=Depth_cuts,col=Depth_cols,legend=FALSE,axes=FALSE,mar=c(6,0,0,0))
#Add coastline
plot(Coast[Coast$ID=='48.6',],col='grey',lwd=0.01,add=T)
#Create pies
=create_Pies(Input=PieData,
MyPiesNamesIn=c("Lat","Lon","Sp","N"),
Size=50
)#Plot Pies
plot(st_geometry(MyPies),col=MyPies$col,add=TRUE)
#Add Pies legend
add_PieLegend(Pies=MyPies,PosX=-0.1,PosY=-1.6,Boxexp=c(0.5,0.45,0.12,0.45),
PieTitle="Species")
Example 2. Pies of constant size, selected classes displayed:
#Plot the bathymetry (See section 'Local map' where B486 was created)
plot(B486,breaks=Depth_cuts,col=Depth_cols,legend=FALSE,axes=FALSE,mar=c(6,0,0,0))
#Add coastline
plot(Coast[Coast$ID=='48.6',],col='grey',lwd=0.01,add=T)
#Create pies
=create_Pies(Input=PieData,
MyPiesNamesIn=c("Lat","Lon","Sp","N"),
Size=50,
Classes=c("TOP","TOA","ANI")
)#Plot Pies
plot(st_geometry(MyPies),col=MyPies$col,add=TRUE)
#Add Pies legend
add_PieLegend(Pies=MyPies,PosX=-0.1,PosY=-1.6,Boxexp=c(0.6,0.6,0.12,0.55),
PieTitle="Selected species")
Example 3. Pies of constant size, proportions below 25% are grouped in a ‘Other’ class (N.B.: unlike Example 2, the ‘Other’ class may contain classes that are displayed in the legend. Please compare Example 1 and Example 3):
#Plot the bathymetry (See section 'Local map' where B486 was created)
plot(B486,breaks=Depth_cuts,col=Depth_cols,legend=FALSE,axes=FALSE,mar=c(6,0,0,0))
#Add coastline
plot(Coast[Coast$ID=='48.6',],col='grey',lwd=0.01,add=T)
#Create pies
=create_Pies(Input=PieData,
MyPiesNamesIn=c("Lat","Lon","Sp","N"),
Size=50,
Other=25
)#Plot Pies
plot(st_geometry(MyPies),col=MyPies$col,add=TRUE)
#Add Pies legend
add_PieLegend(Pies=MyPies,PosX=-0.1,PosY=-1.6,Boxexp=c(0.55,0.55,0.12,0.45),
PieTitle="Other (%) class")
Example 4. Pies of variable size (here, their area is proportional to ‘Catch’), all classes displayed, horizontal legend:
#Plot the bathymetry (See section 'Local map' where B486 was created)
plot(B486,breaks=Depth_cuts,col=Depth_cols,legend=FALSE,axes=FALSE,mar=c(6,0,0,0))
#Add coastline
plot(Coast[Coast$ID=='48.6',],col='grey',lwd=0.01,add=T)
#Create pies
=create_Pies(Input=PieData,
MyPiesNamesIn=c("Lat","Lon","Sp","N"),
Size=18,
SizeVar="Catch"
)#Plot Pies
plot(st_geometry(MyPies),col=MyPies$col,add=TRUE)
#Add Pies legend
add_PieLegend(Pies=MyPies,PosX=-0.1,PosY=-1.6,Boxexp=c(0.16,0.1,0.1,0.4),
PieTitle="Species",SizeTitle="Catch (t.)")
Example 5. Pies of variable size (here, their area is proportional to ‘Catch’), all classes displayed, vertical legend:
#Plot the bathymetry (See section 'Local map' where B486 was created)
plot(B486,breaks=Depth_cuts,col=Depth_cols,legend=FALSE,axes=FALSE,mar=c(0,0,0,10))
#Add coastline
plot(Coast[Coast$ID=='48.6',],col='grey',lwd=0.01,add=T)
#Create pies
=create_Pies(Input=PieData,
MyPiesNamesIn=c("Lat","Lon","Sp","N"),
Size=18,
SizeVar="Catch"
)#Plot Pies
plot(st_geometry(MyPies),col=MyPies$col,add=TRUE)
#Add Pies legend
add_PieLegend(Pies=MyPies,PosX=2.32,PosY=0.1,Boxexp=c(0.35,0.32,0.02,0.15),
PieTitle="Species",SizeTitle="Catch (t.)",Horiz=FALSE,LegSp=0.6)
Example 6. Pies of constant size, all classes displayed. Too many pies (see next example for solution):
#Plot the bathymetry (See section 'Local map' where B486 was created)
plot(B486,breaks=Depth_cuts,col=Depth_cols,legend=FALSE,axes=FALSE,mar=c(6,0,0,0))
#Add coastline
plot(Coast[Coast$ID=='48.6',],col='grey',lwd=0.01,add=T)
#Create pies
=create_Pies(Input=PieData2,
MyPiesNamesIn=c("Lat","Lon","Sp","N"),
Size=5
)#Plot Pies
plot(st_geometry(MyPies),col=MyPies$col,add=TRUE)
#Add Pies legend
add_PieLegend(Pies=MyPies,PosX=0.4,PosY=-1.5,Boxexp=c(0.5,0.45,0.12,0.45),
PieTitle="Species")
Example 7. Pies of constant size, all classes displayed. Gridded locations (in which case numerical variables in the Input are summed for each grid point):
#Plot the bathymetry (See section 'Local map' where B486 was created)
plot(B486,breaks=Depth_cuts,col=Depth_cols,legend=FALSE,axes=FALSE,mar=c(6,0,0,0))
#Add coastline
plot(Coast[Coast$ID=='48.6',],col='grey',lwd=0.01,add=T)
#Create pies
=create_Pies(Input=PieData2,
MyPiesNamesIn=c("Lat","Lon","Sp","N"),
Size=5,
GridKm=250
)#Plot Pies
plot(st_geometry(MyPies),col=MyPies$col,add=TRUE)
#Add Pies legend
add_PieLegend(Pies=MyPies,PosX=0.4,PosY=-1.3,Boxexp=c(0.5,0.45,0.12,0.45),
PieTitle="Species")
Example 8. Pies of variable size (here, their area is proportional to ‘Catch’), all classes displayed, vertical legend, gridded locations (in which case numerical variables in the Input are summed for each grid point):
#Plot the bathymetry (See section 'Local map' where B486 was created)
plot(B486,breaks=Depth_cuts,col=Depth_cols,legend=FALSE,axes=FALSE,mar=c(0,0,0,10))
#Add coastline
plot(Coast[Coast$ID=='48.6',],col='grey',lwd=0.01,add=T)
#Create pies
=create_Pies(Input=PieData2,
MyPiesNamesIn=c("Lat","Lon","Sp","N"),
Size=3,
GridKm=250,
SizeVar='Catch'
)#Plot Pies
plot(st_geometry(MyPies),col=MyPies$col,add=TRUE)
#Add Pies legend
add_PieLegend(Pies=MyPies,PosX=2.8,PosY=0.15,Boxexp=c(0.38,0.32,0.08,0.18),
PieTitle="Species",Horiz=FALSE,SizeTitle="Catch (t.)",
SizeTitleVadj=0.8,nSizes=2)
Download the up-to-date spatial layers from the online CCAMLR GIS and load them to your environment.
For details, type:
?load_ASDs ?load_Bathy
#Load ASDs, EEZs, and Coastline
=load_ASDs()
ASDs=load_EEZs()
EEZs=load_Coastline()
Coastline
#Set the figure margins as c(bottom, left, top, right)
par(mai=c(0,0,0,0))
#Plot
plot(st_geometry(ASDs),col='green',border='blue')
plot(st_geometry(EEZs),col='orange',border='purple',add=T)
plot(st_geometry(Coastline),col='grey',add=T)
add_labels(mode='auto',layer='ASDs',fontsize=0.75,col='red')
box()
Since the ‘load_’ functions require an internet connection, users may desire to save layers on their hard drive for offline use. This may be done, at the risk of not having the most up-to-date layers, as follows:
#Load all layers
=load_ASDs()
ASDs=load_EEZs()
EEZs=load_Coastline()
Coastline=load_SSRUs()
SSRUs=load_RBs()
RBs=load_SSMUs()
SSMUs=load_MAs()
MAs=load_MPAs()
MPAs
#Save as .RData file (here in the temp directory, but users might want to chose their own directory)
save(list=c('ASDs','EEZs','Coastline','SSRUs','RBs','SSMUs','MAs','MPAs'),
file = file.path(tempdir(), "CCAMLRLayers.RData"), compress='xz')
#Later, when offline load layers:
load(file.path(tempdir(), "CCAMLRLayers.RData"))
The load_Bathy() function may also be used to download and store bathymetry data for later use, see ?load_Bathy for details.
Given a bathymetry raster and an input dataframe of latitudes/longitudes, this function computes the depths at these locations. The examples below use the ‘SmallBathy’ data for illustrative purposes; users should use a higher resolution bathymetry dataset instead, as obtained via the load_Bathy() function.
For details, type:
?get_depths
#Generate a dataframe
=data.frame(Lat=PointData$Lat,
MyDataLon=PointData$Lon,
Catch=PointData$Catch)
#The input data looks like this:
head(MyData)
#> Lat Lon Catch
#> 1 -68.63966 -175.0078 53.33002
#> 2 -67.03475 -178.0322 38.66385
#> 3 -65.44164 -170.1656 20.32608
#> 4 -68.36806 151.0247 69.81201
#> 5 -63.89171 154.4327 52.32101
#> 6 -66.35370 153.6906 78.65576
#Get depths of locations
=get_depths(Input=MyData,Bathy=SmallBathy)
MyDataD#The resulting data looks like this (where 'd' is the depth):
head(MyDataD)
#> Lat Lon Catch d
#> 1 -68.63966 -175.0078 53.33002 -3790.7695
#> 2 -67.03475 -178.0322 38.66385 -3959.3145
#> 3 -65.44164 -170.1656 20.32608 -3014.6553
#> 4 -68.36806 151.0247 69.81201 -336.2152
#> 5 -63.89171 154.4327 52.32101 -3234.9985
#> 6 -66.35370 153.6906 78.65576 -1955.7587
#Plot Catch vs Depth
plot(MyDataD$d,MyDataD$Catch,xlab='Depth',ylab='Catch',pch=21,bg='red')
Function to calculate planimetric seabed area within polygons and depth strata in square kilometers. Its accuracy depends on the input bathymetry raster. The examples below use the ‘SmallBathy’ data for illustrative purposes; users should use a higher resolution bathymetry dataset instead, as obtained via the load_Bathy() function. Higher accuracy will be obtained with raw, unprojected bathymetry data.
For details, type:
?seabed_area
#create some polygons
=create_Polys(PolyData,Densify=T)
MyPolys#compute the seabed areas
=seabed_area(SmallBathy,MyPolys,PolyNames="ID",depth_classes=c(0,-200,-600,-1800,-3000,-5000))
FishDepth#Result looks like this (note that the 600-1800 stratum is renamed 'Fishable_area')
head(FishDepth)
#> ID 0|-200 -200|-600 Fishable_area -1800|-3000 -3000|-5000
#> 1 one 0 19100.01 41400.01 40500.01 92700.03
#> 2 two 0 200.00 1800.00 9300.00 93300.03
#> 3 three 700 1600.00 8100.00 229400.07 138000.04
Given a set of polygons and a set of point locations (given in decimal degrees), finds in which polygon those locations fall. Finds, for example, in which ASD the given fishing locations occurred.
For details, type:
?assign_areas
#Generate a dataframe with random locations
=data.frame(Lat=runif(100,min=-65,max=-50),
MyDataLon=runif(100,min=20,max=40))
#The input data looks like this:
head(MyData)
#> Lat Lon
#> 1 -53.11870 34.20721
#> 2 -55.81513 25.69306
#> 3 -61.87161 29.29898
#> 4 -64.10882 34.58778
#> 5 -55.62069 22.56286
#> 6 -52.94103 38.68591
#load ASDs and SSRUs
=load_ASDs()
ASDs=load_SSRUs()
SSRUs
#Assign ASDs and SSRUs to these locations
=assign_areas(MyData,Polys=c('ASDs','SSRUs'),NamesOut=c('MyASDs','MySSRUs'))
MyData#The output data looks like this:
head(MyData)
#> Lat Lon MyASDs MySSRUs
#> 1 -53.11870 34.20721 58.4.4a 58.4.4a D
#> 2 -55.81513 25.69306 48.6 48.6 G
#> 3 -61.87161 29.29898 48.6 48.6 F
#> 4 -64.10882 34.58778 58.4.2 58.4.2 A
#> 5 -55.62069 22.56286 48.6 48.6 G
#> 6 -52.94103 38.68591 58.4.4a 58.4.4a D
#count of locations per ASD
table(MyData$MyASDs)
#>
#> 48.6 58.4.2 58.4.4a
#> 53 7 40
#count of locations per SSRU
table(MyData$MySSRUs)
#>
#> 48.6 F 48.6 G 58.4.2 A 58.4.4a D
#> 17 36 7 40
A simple function to project user-supplied locations. Input must be a dataframe, outputs may be appended to the dataframe.
For details, type:
?project_data
#The input data looks like this:
head(PointData)
#> Lat Lon name Catch Nfishes n
#> 1 -68.63966 -175.0078 one 53.33002 460 1
#> 2 -67.03475 -178.0322 two 38.66385 945 2
#> 3 -65.44164 -170.1656 two 20.32608 374 3
#> 4 -68.36806 151.0247 two 69.81201 87 4
#> 5 -63.89171 154.4327 three 52.32101 552 5
#> 6 -66.35370 153.6906 four 78.65576 22 6
#Generate a dataframe with random locations
=project_data(Input=PointData,NamesIn=c('Lat','Lon'),
MyDataNamesOut=c('Projected_Y','Projected_X'),append=TRUE)
#The output data looks like this:
head(MyData)
#> Lat Lon name Catch Nfishes n Projected_Y Projected_X
#> 1 -68.63966 -175.0078 one 53.33002 460 1 -2361962 -206321.41
#> 2 -67.03475 -178.0322 two 38.66385 945 2 -2545119 -87445.72
#> 3 -65.44164 -170.1656 two 20.32608 374 3 -2680488 -464656.29
#> 4 -68.36806 151.0247 two 69.81201 87 4 -2100218 1162986.84
#> 5 -63.89171 154.4327 three 52.32101 552 5 -2606157 1246832.20
#> 6 -66.35370 153.6906 four 78.65576 22 6 -2349505 1161675.96
Get Cartesian coordinates of lines intersection in Euclidean space. This may have several uses, including when creating polygons with shared boundaries. Uses the coordinates of line extremities as input.
For details, type:
?get_C_intersection
#Prepare layout for 4 sub-plots
par(mfrow=c(2,2),mai=c(0.8,0.8,0.2,0.05))
#Example 1 (Intersection beyond the range of segments)
get_C_intersection(Line1=c(-30,-55,-29,-50),Line2=c(-50,-60,-40,-60))
#> Lon Lat
#> -31 -60
text(-40,-42,"Example 1",xpd=T)
box()
#Example 2 (Intersection on one of the segments)
get_C_intersection(Line1=c(-30,-65,-29,-50),Line2=c(-50,-60,-40,-60))
#> Lon Lat
#> -29.66667 -60.00000
text(-40,-41,"Example 2",xpd=T)
box()
#Example 3 (Crossed segments)
get_C_intersection(Line1=c(-30,-65,-29,-50),Line2=c(-50,-60,-25,-60))
#> Lon Lat
#> -29.66667 -60.00000
text(-38,-41,"Example 3",xpd=T)
box()
#Example 4 (Antimeridian crossed)
get_C_intersection(Line1=c(-179,-60,-150,-50),Line2=c(-120,-60,-130,-62))
#> Warning in get_C_intersection(Line1 = c(-179, -60, -150, -50), Line2 =
#> c(-120, : Antimeridian crossed. Find where your line crosses it first, using
#> Line=c(180,-90,180,0) or Line=c(-180,-90,-180,0).
#> Lon Lat
#> -260.47619 -88.09524
text(-180,-37,"Example 4",xpd=T)
box()
Coloring bathymetry requires a vector of depth classes and a vector of colors. Colors are applied between depth classes (so there is one less color than there are depth classes). Two sets of bathymetry colors are included in the package. One simply colors the bathymetry in shades of blue (Depth_cols and Depth_cuts), the other adds shades of green to highlight the Fishable Depth range (600-1800m; Depth_cols2 and Depth_cuts2). The examples below use the ‘SmallBathy’ data for illustrative purposes; users should use a higher resolution bathymetry dataset instead, as obtained via the load_Bathy() function.
#Set the figure margins as c(bottom, left, top, right)
par(mai=c(0,0.4,0,0))
#Plot the bathymetry
plot(SmallBathy,breaks=Depth_cuts,col=Depth_cols,axes=FALSE,box=FALSE,legend=FALSE)
#Add color scale
add_Cscale(cuts=Depth_cuts,cols=Depth_cols,fontsize=0.75,height=80,offset=-500,width=16,maxVal=-1)
#Set the figure margins as c(bottom, left, top, right)
par(mai=c(0,0.4,0,0))
#Plot the bathymetry
plot(SmallBathy,breaks=Depth_cuts2,col=Depth_cols2,axes=FALSE,box=FALSE,legend=FALSE)
#Add color scale
add_Cscale(cuts=Depth_cuts2,cols=Depth_cols2,fontsize=0.75,height=80,offset=-500,width=16,maxVal=-1)
Adding colors to plots revolves around two functions:
?add_col#and
?add_Cscale
add_col() generates colors for a variable of interest as well as a set of color classes and colors to be used as inputs to the add_Cscale() function. Colors and color classes may be generated automatically or customized, depending on the intended appearance. Knowing the names of colors in R would be useful here (http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf).
#Adding color to points
#Prepare layout for 3 sub-plots
par(mfrow=c(3,1),mai=c(0.1,0.1,0.1,1))
#Create some points
=create_Points(PointData)
MyPoints
#Example 1: Add default cols and cuts
=add_col(MyPoints$Nfishes)
MyColsplot(st_geometry(MyPoints),pch=21,bg=MyCols$varcol,main='Example 1:',cex.main=0.75,cex=1.5,lwd=0.5)
box()
add_Cscale(title='Number of fishes',
height=95,fontsize=0.75,width=16,lwd=1,offset=0,
cuts=MyCols$cuts,cols=MyCols$cols)
#Example 2: Given the look of example 1, reduce the number of cuts and round their values (in add_Cscale)
=add_col(MyPoints$Nfishes,cuts=10)
MyColsplot(st_geometry(MyPoints),pch=21,bg=MyCols$varcol,main='Example 2:',cex.main=0.75,cex=1.5,lwd=0.5)
box()
add_Cscale(title='Number of fishes',
height=95,fontsize=0.75,width=16,lwd=1,offset=0,
cuts=round(MyCols$cuts,1),cols=MyCols$cols)
#Example 3: same as example 2 but with custom colors
=add_col(MyPoints$Nfishes,cuts=10,cols=c('black','yellow','purple','cyan'))
MyColsplot(st_geometry(MyPoints),pch=21,bg=MyCols$varcol,main='Example 3:',cex.main=0.75,cex=1.5,lwd=0.5)
add_Cscale(title='Number of fishes',
height=95,fontsize=0.75,width=16,lwd=1,offset=0,
cuts=round(MyCols$cuts,1),cols=MyCols$cols)
box()
#Adding colors to a grid with custom cuts (see also the last example in section 2.1.)
#Step 1: Generate your grid
=create_PolyGrids(GridData,Area=10000)
MyGrid
#Step 2: Inspect your gridded data (e.g. hist(MyGrid$Catch_sum,100))
#to determine whether irregular cuts are required.
#In this case (heterogeneously distributed data) irregular cuts
#would be preferable, such as:
=c(0,50,100,500,2000,2500)
MyCuts
#Step 3: Generate colors according to the desired classes (MyCuts)
=add_col(MyGrid$Catch_sum,cuts=MyCuts,cols=c('blue','white','red'))
Gridcol
#Step 4: Plot result and add color scale
par(mai=c(0,0,0,1.5)) #set plot margins as c(bottom, left, top, right)
#Use the colors generated by add_col
plot(st_geometry(MyGrid),col=Gridcol$varcol,lwd=0.1)
#Add color scale using cuts and cols generated by add_col
add_Cscale(title='Sum of Catch (t)',cuts=Gridcol$cuts,cols=Gridcol$cols,width=22,
fontsize=0.75,lwd=1)
To add a legend, use the base legend() function:
?legend
To position the legend, the add_Cscale() function can generate legend coordinates which correspond to the top-left corner of the legend box. These may be adjusted using the ‘pos’, ‘height’ and ‘offset’ arguments within add_Cscale(), e.g.:
=add_Cscale(pos='2/3',offset=1000,height=40,mode="Legend") Legend_Coordinates
#Adding a color scale and a legend
#Create some point data
=create_Points(PointData)
MyPoints
#Crop the bathymetry to match the extent of MyPoints (extended extent)
=crop(rast(SmallBathy),extend(ext(MyPoints),100000))
BathyCr#Plot the bathymetry
plot(BathyCr,breaks=Depth_cuts,col=Depth_cols,legend=F,axes=F,mar=c(0,0,0,7))
#Add a color scale
add_Cscale(pos='3/8',height=50,maxVal=-1,minVal=-4000,fontsize=0.75,lwd=1,width=16)
#Plot points with different symbols and colors (see ?points)
=c(21,22,23,24)
Psymbols=c('red','green','blue','yellow')
Pcolorsplot(st_geometry(MyPoints[MyPoints$name=='one',]),pch=Psymbols[1],bg=Pcolors[1],add=T)
plot(st_geometry(MyPoints[MyPoints$name=='two',]),pch=Psymbols[2],bg=Pcolors[2],add=T)
plot(st_geometry(MyPoints[MyPoints$name=='three',]),pch=Psymbols[3],bg=Pcolors[3],add=T)
plot(st_geometry(MyPoints[MyPoints$name=='four',]),pch=Psymbols[4],bg=Pcolors[4],add=T)
#Add legend with position determined by add_Cscale
=add_Cscale(pos='7/8',height=40,mode='Legend')
Loclegend(Loc,legend=c('one','two','three','four'),
title='Vessel',pch=Psymbols,pt.bg=Pcolors,xpd=T,
box.lwd=1,cex=0.75,pt.cex=1,y.intersp=1.5)
To add labels, use the add_labels() function:
?add_labels
Three modes are available within the add_labels() function:
#Example 1: 'auto' mode
#label ASDs in bold and red
=load_ASDs()
ASDs#set plot margins as c(bottom, left, top, right)
par(mai=c(0,0,0,0))
plot(st_geometry(ASDs))
add_labels(mode='auto',layer='ASDs',fontsize=0.75,fonttype=2,col='red')
#add MPAs and EEZs and their labels in large, green and vertical text
=load_MPAs()
MPAs=load_EEZs()
EEZsplot(st_geometry(MPAs),add=TRUE,border='green')
plot(st_geometry(EEZs),add=TRUE,border='green')
add_labels(mode='auto',layer=c('EEZs','MPAs'),fontsize=1,col='green',angle=90)
#Example 2: 'auto' and 'input' modes
#This example is not executed here because it needs user interaction.
#Please copy and paste it in the Console to see how it works.
#Prepare a basemap
plot(SmallBathy)
=load_ASDs()
ASDsplot(st_geometry(ASDs),add=T)
#Build your labels
=add_labels(mode='manual')
MyLabels
#Re-use the label table generated (if desired)
plot(SmallBathy)
plot(st_geometry(ASDs),add=T)
add_labels(mode='input',LabelTable=MyLabels)
Depending on the function used, the CCAMLRGIS package computes data summaries and includes them in the resulting spatial object. For example, create_Polys takes any numerical values included in the Input data frame and computes, for each polygon, the minimum, maximum, mean, median, sum, count and standard deviation of values associated with each polygon. The sf package has some useful plotting methods, some of which are shown below.
#First, let's create some example polygons
=create_Polys(PolyData)
MyPolys
#MyPolys is an sf object; it is a data frame that includes a column named 'geometry':
::kable(MyPolys,row.names = F) kableExtra
ID | Catch_min | Nfishes_min | n_min | Catch_max | Nfishes_max | n_max | Catch_mean | Nfishes_mean | n_mean | Catch_sum | Nfishes_sum | n_sum | Catch_count | Nfishes_count | n_count | Catch_sd | Nfishes_sd | n_sd | Catch_median | Nfishes_median | n_median | geometry | AreaKm2 | Labx | Laby |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
one | 52.61262 | 11 | 1 | 71.65909 | 329 | 4 | 64.17380 | 172.5000 | 2.5 | 256.6952 | 690 | 10 | 4 | 4 | 4 | 9.084736 | 153.3917 | 1.290994 | 66.21175 | 175.0 | 2.5 | POLYGON ((-290035.9 -164487… | 187281.3 | -170519.8 | -1949051 |
two | 23.12032 | 116 | 5 | 73.49383 | 954 | 8 | 51.94951 | 505.0000 | 6.5 | 207.7980 | 2020 | 26 | 4 | 4 | 4 | 22.264999 | 428.9188 | 1.290994 | 55.59195 | 475.0 | 6.5 | POLYGON ((-423880.7 -240394… | 95294.2 | 0.0 | -2483470 |
three | 10.23393 | 13 | 9 | 95.57774 | 988 | 14 | 52.50313 | 412.3333 | 11.5 | 315.0188 | 2474 | 69 | 6 | 6 | 6 | 32.152675 | 382.8685 | 1.870829 | 54.15367 | 341.5 | 11.5 | POLYGON ((480755.1 -2726497… | 361556.2 | 786933.1 | -2846388 |
The ‘geometry’ column contains the locations of each point of a given polygon (each row), and can be plotted using plot(st_geometry(MyPolys)), as shown previously in this document. Alternatively, one can use plot(MyPolys) directly:
plot(MyPolys)
#> Warning: plotting the first 9 out of 25 attributes; use max.plot = 25 to plot
#> all
This results in a warning Warning: plotting the first 9 out of 25 attributes… and a 9-panel plot as shown above, with each panel corresponding to each column present in MyPolys and automatic colors generated according to the values in each column. In order to plot only one variable, it must be named in the plotting command:
plot(MyPolys["Catch_mean"])
There are several available options, for example:
=st_graticule(MyPolys,lon=seq(-180,180,by=5),lat=seq(-80,0,by=2.5))
Grplot(MyPolys["Catch_mean"],
graticule=Gr,axes=T,key.pos=1,key.width=0.2,key.length=0.8,breaks=seq(50,65,by=2.5))
Where:
key.pos controls the color legend position as 1=below, 2=left, 3=above and 4=right,
key.width and key.length control the size of the color legend,
breaks controls the classes,
The function st_graticule generates a Lat/Lon grid.
Additionally, sf objects can be plotted using ggplot2. For example:
library(ggplot2)
ggplot() +
geom_sf(data = MyPolys, aes(fill = Catch_mean))
Using ggplot2 and gridExtra, multi-panel plots can be drawn:
library(gridExtra)
<- ggplot() +
map1 geom_sf(data = MyPolys, aes(fill = Catch_mean)) +
labs(title="Mean catch")
<- ggplot() +
map2 geom_sf(data = MyPolys, aes(fill = Catch_sd)) +
labs(title="S.D. of catch")
<- ggplot() +
map3 geom_sf(data = MyPolys, aes(fill = AreaKm2)) +
labs(title="Polygon area")
grid.arrange(map1, map2, map3, ncol=2)