EVOLUTION-MANAGER
Edit File: addLegend.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Add a color legend to a map</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for addLegend {leaflet}"><tr><td>addLegend {leaflet}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Add a color legend to a map</h2> <h3>Description</h3> <p>When a color palette function is used in a map (e.g. <code><a href="colorNumeric.html">colorNumeric</a></code>), a color legend can be automatically derived from the palette function. You can also manually specify the colors and labels for the legend. </p> <h3>Usage</h3> <pre> addLegend(map, position = c("topright", "bottomright", "bottomleft", "topleft"), pal, values, na.label = "NA", bins = 7, colors, opacity = 0.5, labels = NULL, labFormat = labelFormat(), title = NULL, className = "info legend", layerId = NULL, group = NULL, data = getMapData(map)) labelFormat(prefix = "", suffix = "", between = " &ndash; ", digits = 3, big.mark = ",", transform = identity) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>map</code></td> <td> <p>a map widget object created from <code><a href="leaflet.html">leaflet</a>()</code></p> </td></tr> <tr valign="top"><td><code>position</code></td> <td> <p>the position of the legend</p> </td></tr> <tr valign="top"><td><code>pal</code></td> <td> <p>the color palette function, generated from <code><a href="colorNumeric.html">colorNumeric</a>()</code>, <code>colorBin()</code>, <code>colorQuantile()</code>, or <code>colorFactor()</code></p> </td></tr> <tr valign="top"><td><code>values</code></td> <td> <p>the values used to generate colors from the palette function</p> </td></tr> <tr valign="top"><td><code>na.label</code></td> <td> <p>the legend label for <code>NA</code>s in <code>values</code></p> </td></tr> <tr valign="top"><td><code>bins</code></td> <td> <p>an approximate number of tick-marks on the color gradient for the <code>colorNumeric</code> palette if it is of length one; you can also provide a numeric vector as the pre-defined breaks (equally spaced)</p> </td></tr> <tr valign="top"><td><code>colors</code></td> <td> <p>a vector of (HTML) colors to be used in the legend if <code>pal</code> is not provided</p> </td></tr> <tr valign="top"><td><code>opacity</code></td> <td> <p>the opacity of colors</p> </td></tr> <tr valign="top"><td><code>labels</code></td> <td> <p>a vector of text labels in the legend corresponding to <code>colors</code></p> </td></tr> <tr valign="top"><td><code>labFormat</code></td> <td> <p>a function to format the labels derived from <code>pal</code> and <code>values</code> (see Details below to know what <code>labelFormat()</code> returns by default; you can either use the helper function <code>labelFormat()</code>, or write your own function)</p> </td></tr> <tr valign="top"><td><code>title</code></td> <td> <p>the legend title</p> </td></tr> <tr valign="top"><td><code>className</code></td> <td> <p>extra CSS classes to append to the control, space separated</p> </td></tr> <tr valign="top"><td><code>layerId</code></td> <td> <p>the ID of the legend; subsequent calls to <code>addLegend</code> or <code>addControl</code> with the same <code>layerId</code> will replace this legend. The ID can also be used with <code>removeControl</code>.</p> </td></tr> <tr valign="top"><td><code>group</code></td> <td> <p><code>group</code> name of a leaflet layer group. Supplying this value will tie the legend to the leaflet layer group with this name and will auto add/remove the legend as the group is added/removed, for example via layerControl. You will need to set the <code>group</code> when you add a layer (e.g. <code><a href="map-layers.html">addPolygons</a></code>) and supply the same name here.</p> </td></tr> <tr valign="top"><td><code>data</code></td> <td> <p>the data object from which the argument values are derived; by default, it is the <code>data</code> object provided to <code>leaflet()</code> initially, but can be overridden</p> </td></tr> <tr valign="top"><td><code>prefix</code></td> <td> <p>a prefix of legend labels</p> </td></tr> <tr valign="top"><td><code>suffix</code></td> <td> <p>a suffix of legend labels</p> </td></tr> <tr valign="top"><td><code>between</code></td> <td> <p>a separator between <code>x[i]</code> and <code>x[i + 1]</code> in legend labels (by default, it is a dash)</p> </td></tr> <tr valign="top"><td><code>digits</code></td> <td> <p>the number of digits of numeric values in labels</p> </td></tr> <tr valign="top"><td><code>big.mark</code></td> <td> <p>the thousand separator</p> </td></tr> <tr valign="top"><td><code>transform</code></td> <td> <p>a function to transform the label value</p> </td></tr> </table> <h3>Details</h3> <p>The <code>labFormat</code> argument is a function that takes the argument <code>type = c("numeric", "bin", "quantile", "factor")</code>, plus, arguments for different types of color palettes. For the <code>colorNumeric()</code> palette, <code>labFormat</code> takes a single argument, which is the breaks of the numeric vector, and returns a character vector of the same length. For <code>colorBin()</code>, <code>labFormat</code> also takes a vector of breaks of length <code>n</code> but should return a character vector of length <code>n - 1</code>, with the <code>i</code>-th element representing the interval <code>c(x[i], x[i + 1])</code>. For <code>colorQuantile</code>, <code>labFormat</code> takes two arguments, the quantiles and the associated probabilities (each of length <code>n</code>), and should return a character vector of length <code>n - 1</code> (similar to the <code>colorBin()</code> palette). For <code>colorFactor()</code>, <code>labFormat</code> takes one argument, the unique values of the factor, and should return a character vector of the same length. </p> <p>By default, <code>labFormat</code> is basically <code>format(scientific = FALSE, big.mark = ",")</code> for the numeric palette, <code>as.character()</code> for the factor palette, and a function to return labels of the form <span class="samp">x[i] - x[i + 1]</span> for bin and quantile palettes (in the case of quantile palettes, <code>x</code> is the probabilities instead of the values of breaks). </p> <h3>Examples</h3> <pre> # !formatR library(leaflet) # a manual legend leaflet() %>% addTiles() %>% addLegend( position = "bottomright", colors = rgb(t(col2rgb(palette())) / 255), labels = palette(), opacity = 1, title = "An Obvious Legend" ) # an automatic legend derived from the color palette df <- local({ n <- 300; x <- rnorm(n); y <- rnorm(n) z <- sqrt(x ^ 2 + y ^ 2); z[sample(n, 10)] <- NA data.frame(x, y, z) }) pal <- colorNumeric("OrRd", df$z) leaflet(df) %>% addTiles() %>% addCircleMarkers(~x, ~y, color = ~pal(z), group = "circles") %>% addLegend(pal = pal, values = ~z, group = "circles", position = "bottomleft") %>% addLayersControl(overlayGroups = c("circles")) # format legend labels df <- data.frame(x = rnorm(100), y = rexp(100, 2), z = runif(100)) pal <- colorBin("PuOr", df$z, bins = c(0, .1, .4, .9, 1)) leaflet(df) %>% addTiles() %>% addCircleMarkers(~x, ~y, color = ~pal(z), group = "circles") %>% addLegend(pal = pal, values = ~z, group = "circles", position = "bottomleft") %>% addLayersControl(overlayGroups = c("circles")) leaflet(df) %>% addTiles() %>% addCircleMarkers(~x, ~y, color = ~pal(z), group = "circles") %>% addLegend(pal = pal, values = ~z, labFormat = labelFormat( prefix = "(", suffix = ")%", between = ", ", transform = function(x) 100 * x ), group = "circles", position = "bottomleft" ) %>% addLayersControl(overlayGroups = c("circles")) </pre> <hr /><div style="text-align: center;">[Package <em>leaflet</em> version 2.0.3 <a href="00Index.html">Index</a>]</div> </body></html>