add_Legend: Add Legend

View source: R/add_Legend.R

add_LegendR Documentation

Add Legend

Description

Add a legend to you map. Give the bounding box of your plot and lists of parameters as inputs.

Usage

add_Legend(bb, LegOpt, Items)

Arguments

bb

bounding box of your plot area. for example, bb=st_bbox(SmallBathy()) or bb=st_bbox(MyPolys).

LegOpt

list of general legend options. for example:

LegOpt=list(Title="Speed",Subtitle="(km/h)").

Items

list, or list of lists containing options for each item to be displayed in the legend. for example:

item1=list(Text="one",Shape="rectangle")

item2=list(Text="two",Shape="circle")

Items=list(item1,item2)

Value

Legend added to current plot.

LegOpt options

  • Title: character, title of the legend, set to NULL for no title.

  • Subtitle: character, subtitle of the legend, set to NULL for no subtitle.

  • Pos: character, general position of the legend. One of "bottomright" (default), "bottom", "bottomleft", "left", "topleft", "top", "topright", "right" or "center".

  • BoxW: numeric, legend box width (see figure below).

  • BoxH: numeric, legend box height (see figure below).

  • PosX: numeric, horizontal adjustment of legend (see figure below).

  • PosY: numeric, vertical adjustment of legend (see figure below).

  • Boxexp: numeric, vector of length 4 controlling the expansion of the legend box, given as c(xmin,xmax,ymin,ymax), see figure below.

  • Boxbd: character, color of the background of the legend box. set to NA for no background.

  • Boxcol: character, color of the border of the legend box. Set to NA for no box.

  • Boxlwd: numeric, line thickness of the legend box. Set Boxcol to NA for no box.

  • Titlefontsize: numeric, size of the legend title.

  • Subtitlefontsize: numeric, size of the legend subtitle.

  • TitleAdj: numeric vector of length 2, as c(x,y) to adjust title location (see figure below).

  • SubtitleAdj: numeric vector of length 2, as c(x,y) to adjust subtitle location.

Items options that are common to all items

  • Text: character, text of the item.

  • Shape: character, shape description, one of "rectangle", "circle", "ellipse", "line", "arrow" or "none". Using "none" will leave a blank space that can be filled by a user-defined shape.

  • ShpFill: character, fill color of shape, set to NA for no fill.

  • ShpBord: character, border color of shape, set to NA for no border.

  • ShpHash: logical (TRUE/FALSE) to add hashed lines to the shape (see create_Hashes).

  • Shplwd: numeric, line thickness of the shape's border, set ShpBord to NA for no border.

  • fontsize: numeric, size of the text.

  • STSpace: numeric, space between the Shape and its Text (see figure below).

  • ShiftX: numeric, shift Shape and Text left or right (see figure below).

  • ShiftY: numeric, shift Shape and Text up or down (see figure below).

  • Hashcol: character, color of hashes (if ShpHash is TRUE), see create_Hashes for details.

  • Hashangle: numeric, angle of hashes (if ShpHash is TRUE), see create_Hashes for details.

  • Hashspacing: numeric, spacing between hashes (if ShpHash is TRUE), see create_Hashes for details.

  • Hashwidth: numeric, width of hashes (if ShpHash is TRUE), see see create_Hashes for details.

Items options that are specific to the item's Shape

  • RectW: numeric, width of rectangle shape.

  • RectH: numeric, height of rectangle shape.

  • CircD: numeric, diameter of circle shape.

  • EllW: numeric, width of ellipse shape.

  • EllH: numeric, height of ellipse shape.

  • EllA: numeric, angle of ellipse shape.

  • LineTyp: numeric, type of line shape (0=blank, 1=solid, 2=dashed, 3=dotted, 4=dotdash, 5=longdash, 6=twodash).

  • LineL: numeric, length of the line shape.

  • ArrL: numeric, length of the arrow shape.

  • ArrPwidth: numeric, width of arrow's path. see create_Arrow for details.

  • ArrHlength: numeric, length of arrow's head. see create_Arrow for details.

  • ArrHwidth: numeric, width of arrow's head. see create_Arrow for details.

  • Arrdlength: numeric, length of dashes for dashed arrows. see create_Arrow for details.

  • Arrtype: character, arrow type either "normal" or "dashed". see create_Arrow for details.

  • Arrcol: character, color of the arrow. see create_Arrow for details.

  • Arrtrans: numeric, transparency of the arrow. see create_Arrow for details.

The figure below shows some of the options used to customize the legend box and its items. Blue arrows represent location options and black arrows represent sizing options:

Addlegenddetails.png

See Also

create_Hashes, create_Arrow, create_Ellipse, add_labels, add_Cscale, add_PieLegend.

Examples


# For more examples, see:
# https://github.com/ccamlr/CCAMLRGIS#53-adding-legends

# Set general options:

LegOpt=list( 
Title= "Title",
Subtitle="(Subtitle)",
Pos = "bottomright",
BoxW= 80,
BoxH= 170,
Boxexp = c(5,-2,-4,-4),
Titlefontsize = 2
)


#Create separate items, each with their own options:


Rectangle1=list(
  Text="Rectangle 1", 
  Shape="rectangle",
  ShpFill="cyan",
  ShpBord="blue",
  Shplwd=2,
  fontsize=1.2,
  STSpace=3,
  RectW=10,
  RectH=7
)

Rectangle2=list(
  Text="Rectangle 2", 
  Shape="rectangle",
  ShpFill="red",
  ShpBord="orange",
  ShpHash=TRUE,
  Shplwd=2,
  fontsize=1.2,
  STSpace=3,
  RectW=10,
  RectH=7,
  Hashcol="white",
  Hashangle=45,
  Hashspacing=1,
  Hashwidth=1
)

Circle1=list(
  Text="Circle 1", 
  Shape="circle",
  ShpFill="grey",
  ShpBord="yellow",
  Shplwd=2,
  fontsize=1.2,
  STSpace=3,
  CircD=10
)

Circle2=list(
  Text="Circle 2", 
  Shape="circle",
  ShpFill="white",
  ShpBord="red",
  ShpHash=TRUE,
  Shplwd=2,
  fontsize=1.2,
  STSpace=3,
  CircD=10,
  Hashcol="black",
  Hashangle=0,
  Hashspacing=2,
  Hashwidth=2
)

Ellipse1=list(
  Text="Ellipse 1", 
  Shape="ellipse",
  ShpFill="white",
  ShpBord="darkblue",
  Shplwd=2,
  fontsize=1.2,
  STSpace=3,
  EllW=10,
  EllH=6,
  EllA=35
)

Ellipse2=list(
  Text="Ellipse 2", 
  Shape="ellipse",
  ShpFill="red",
  ShpBord="green",
  ShpHash=TRUE,
  Shplwd=2,
  fontsize=1.2,
  STSpace=3,
  EllW=10,
  EllH=7,
  EllA=0,
  Hashcol="black",
  Hashangle=-45,
  Hashspacing=1.5,
  Hashwidth=1.5
)

Line1=list(
  Text="Line 1", 
  Shape="line",
  ShpFill="black",
  Shplwd=5,
  fontsize=1.2,
  STSpace=3,
  LineL=10
)

Line2=list(
  Text="Line 2", 
  Shape="line",
  Shplwd=5,
  ShpFill="green",
  Shplwd=5,
  fontsize=1.2,
  STSpace=3,
  LineTyp=6, 
  LineL=10
)

Arrow1=list(
  Text="Arrow 1", 
  Shape="arrow",
  ShpBord="green",
  Shplwd=1,
  ArrL=10,
  ArrPwidth=5,
  ArrHlength=15, 
  ArrHwidth=10, 
  Arrcol="orange",
  fontsize=1.2,
  STSpace=3
)

Arrow2=list(
  Text="Arrow 2", 
  Shape="arrow",
  ShpBord=NA,
  ArrL=10,
  ArrPwidth=5,
  ArrHlength=15, 
  ArrHwidth=10, 
  Arrdlength=0, 
  Arrtype="dashed",
  Arrcol=c("red","green","blue"),
  fontsize=1.2,
  STSpace=3
)

Arrow3=list(
  Text="Arrow 3", 
  Shape="arrow",
  ShpBord=NA,
  ArrL=10,
  ArrPwidth=5,
  ArrHlength=15, 
  ArrHwidth=10, 
  Arrdlength=5, 
  Arrtype="dashed",
  Arrcol="darkgreen",
  fontsize=1.2,
  STSpace=3
)

Arrow4=list(
  Text="Arrow 4", 
  Shape="arrow",
  ShpBord="black",
  Shplwd=0.1,
  ArrL=10,
  ArrPwidth=5,
  ArrHlength=15, 
  ArrHwidth=10, 
  Arrcol="pink",
  ShpHash=TRUE,
  Hashcol="blue",
  Hashangle=-45,
  Hashspacing=1,
  Hashwidth=1,
  fontsize=1.2,
  STSpace=3
)

None=list(
  Text="None", 
  Shape="none",
  fontsize=1.2,
  STSpace=3,
  ShiftX=10
)


#Combine all items into a single list:

Items=list(Rectangle1,Rectangle2,Circle1,Circle2,
Ellipse1,Ellipse2,Line1,Line2,Arrow1,Arrow2,Arrow3,Arrow4,None)

#manually build a bounding box (same as st_bbox(load_ASDs())):

bb=st_bbox(c(xmin=-3348556,xmax=4815055,ymax=4371127,ymin=-3329339),
           crs = st_crs(6932))
bx=st_as_sfc(bb) #Convert to polygon to plot it

#Plot and add legend

plot(bx,col="grey")
add_Legend(bb,LegOpt,Items)


CCAMLRGIS documentation built on April 3, 2025, 10:53 p.m.