legend(x, y, legend, angle = <<see below>>, density = <<see below>>, fill = <<see below>>, col = <<see below>>, lty = <<see below>>, lwd = <<see below>>, marks = <<see below>>, pch = <<see below>>, ncol = 1, background = 0)
Graphical parameters may also be supplied as arguments to this function (see par). In particular, the bty parameter may be used to suppress the box surrounding the legend (bty="n").
Symbols in the legend can be specified in several different ways, depending on whether you choose to plot common plotting characters, letters or keyboard symbols (see below and the EXAMPLES section for details).
1) Symbols can be specified in the marks argument as a numeric vector of decimal numbers. The decimal number corresponding to a particular symbol can be found using the AsciiToInt function.
2) A combination of the marks and pch arguments can be used to specify plotting symbols. Use a negative number in marks every place you want to use a character in pch, and a blank space in pch every place you want to use a symbol in marks.
3) Symbols can be specified in the pch argument using a character string. In this case, all of the symbols have to be represented as characters, which can be done by converting them to three digit octal numbers preceded by a backslash. for example, pch="\011" is pch=9 and pch="\102" is pch="B".
# locator allows you to click on plot to to point at upper-left corner # of area to contain the legend -- draw colored boxes plot(freeny.x[,1], ylim = c(1:10), pch = 15, col = 2) points(freeny.x[,2], pch = 15, col = 3) points(freeny.x[,3], pch = 15, col = 4) typ.names <- c("price index", "income level", "market potential") legend(locator(1), legend = typ.names, fill = 2:4)# draw legend with different line styles and plotting chars tsplot(bonds.yield[1:40,], lty = 1:6) par(col = 3) # make the legend in color 3 legend(13, .086, legend = as.character(bonds.coupon), lty = 1:6, pch = "OXAC*I")
# suppress the bounding box for the legend legend(13, .086, legend = as.character(bonds.coupon), lty = 1:6, pch = "OXAC*I", bty = "n")
plot(testscores[,1], pch = 8) # use a decimal representation points(testscores[,2], pch = "7") # plot a literal character points(testscores[,3], pch = "\046") # plot an ampersand ("&") # using octal representation
# the legend can be written (at least!) three different ways:
legend(1,20,c("DG","C","A"), pch = "\0107\046") # using pch only, # here "\010" is the octal representation of pch=8.
legend(1,20,c("DG","C","A"), marks = c(8,-1,-1), pch = " 7\046") # the desired symbol is either represented in marks with a # blank space in pch or by pch with a "-1" in marks.
# use AsciiToInt to find decimal value for ascii characters: AsciiToInt("7","&")
legend(1,20,c("DG","C","A"), marks = c(8,55,38)) # literal "7" # and octal \046 (ampersand) map to decimal values in marks