# For more examples, see:
# https://github.com/ccamlr/CCAMLRGIS#5-adding-colors-legends-and-labels
library(terra)
#Example 1: Adding two color scales
plot(SmallBathy(),breaks=Depth_cuts,col=Depth_cols,legend=FALSE,axes=FALSE,box=FALSE)
add_Cscale(pos='1/2',height=45,maxVal=0,minVal=-4000,fontsize=0.8)
#Some gridded data
MyGrid=create_PolyGrids(GridData,dlon=2,dlat=1)
Gridcol=add_col(MyGrid$Catch_sum,cuts=10)
plot(st_geometry(MyGrid),col=Gridcol$varcol,add=TRUE)
#Add color scale using cuts and cols generated by add_col, note the use of 'round'
add_Cscale(pos='2/2',height=45,title='Catch (t)',
cuts=round(Gridcol$cuts,1),cols=Gridcol$cols,fontsize=0.8)
#Example 2: Adding a color scale and a legend
#Create some point data
MyPoints=create_Points(PointData)
#Crop the bathymetry to match the extent of MyPoints
BathyCr=crop(SmallBathy(),extend(ext(MyPoints),100000))
plot(BathyCr,breaks=Depth_cuts,col=Depth_cols,legend=FALSE,axes=FALSE,mar=c(0,0,0,7))
add_Cscale(pos='1/2',height=45,maxVal=0,minVal=-4000,fontsize=0.8)
#Plot points with different symbols and colors (see ?points)
Psymbols=c(21,22,23,24)
Pcolors=c('red','green','blue','yellow')
plot(st_geometry(MyPoints[MyPoints$name=='one',]),pch=Psymbols[1],bg=Pcolors[1],add=TRUE)
plot(st_geometry(MyPoints[MyPoints$name=='two',]),pch=Psymbols[2],bg=Pcolors[2],add=TRUE)
plot(st_geometry(MyPoints[MyPoints$name=='three',]),pch=Psymbols[3],bg=Pcolors[3],add=TRUE)
plot(st_geometry(MyPoints[MyPoints$name=='four',]),pch=Psymbols[4],bg=Pcolors[4],add=TRUE)
#Add legend with position determined by add_Cscale
Loc=add_Cscale(pos='2/2',height=45,mode='Legend')
legend(Loc,legend=c('one','two','three','four'),title='Vessel',pch=Psymbols,
pt.bg=Pcolors,xpd=TRUE)
Run the code above in your browser using DataLab