{"action":"created","issue":{"url":"https://api.github.com/repos/rstudio/leaflet/issues/53","labels_url":"https://api.github.com/repos/rstudio/leaflet/issues/53/labels{/name}","comments_url":"https://api.github.com/repos/rstudio/leaflet/issues/53/comments","events_url":"https://api.github.com/repos/rstudio/leaflet/issues/53/events","html_url":"https://github.com/rstudio/leaflet/pull/53","id":65788755,"number":53,"title":"(WIP) color legends","user":{"login":"yihui","id":163582,"avatar_url":"https://avatars.githubusercontent.com/u/163582?v=3","gravatar_id":"","url":"https://api.github.com/users/yihui","html_url":"https://github.com/yihui","followers_url":"https://api.github.com/users/yihui/followers","following_url":"https://api.github.com/users/yihui/following{/other_user}","gists_url":"https://api.github.com/users/yihui/gists{/gist_id}","starred_url":"https://api.github.com/users/yihui/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yihui/subscriptions","organizations_url":"https://api.github.com/users/yihui/orgs","repos_url":"https://api.github.com/users/yihui/repos","events_url":"https://api.github.com/users/yihui/events{/privacy}","received_events_url":"https://api.github.com/users/yihui/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-04-01T22:02:23Z","updated_at":"2015-04-01T23:32:14Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/rstudio/leaflet/pulls/53","html_url":"https://github.com/rstudio/leaflet/pull/53","diff_url":"https://github.com/rstudio/leaflet/pull/53.diff","patch_url":"https://github.com/rstudio/leaflet/pull/53.patch"},"body":"When a color palette function is created, some metadata is stored as the attributes of the function, which will be used to derive the legend items later in `addLegend()`.\r\n\r\nLegends for factor variables are straightforward. I have not decided how to deal with numeric variables in the legend. I don't think it is a good idea to use the binning methods in the color palettes, since it is more important for the bins to be human-readable in the legend. `pretty()` might make more sense than `cut()`. Some quick examples:\r\n\r\nA manual legend (users specify the colors and labels):\r\n\r\n```r\r\nleaflet() %>% addTiles() %>% addLegend(\r\n  position = 'bottomright',\r\n  colors = rgb(t(col2rgb(palette())) / 255), labels = palette()\r\n)\r\n```\r\n\r\n![screenshot from 2015-04-01 16 43 11](https://cloud.githubusercontent.com/assets/163582/6953432/eb6ae6de-d88e-11e4-90ce-0c023b529468.png)\r\n\r\nDerive legend info from the color palette function:\r\n\r\n```r\r\nlibrary(maps)\r\nmapState = map(\"state\", fill=TRUE, plot=FALSE)\r\nsnames = unique(mapState$names)\r\nsnames[snames == 'new york:main'] = 'new york'\r\nsnames[snames == 'washington:main'] = 'washington'\r\nsnames[grep('^(new york|washington):', snames)] = NA\r\nsnames = gsub(':.+$', '', snames)\r\nsarea = state.area[match(snames, tolower(state.name))]\r\npal = colorNumeric('YlOrRd', sarea)\r\n\r\nleaflet(data = mapState) %>% addTiles() %>%\r\n  addPolygons(fillColor = pal(sarea), stroke = FALSE, fillOpacity = 0.5) %>%\r\n  addLegend(pal = pal, values = sarea, bins = 10)\r\n```\r\n\r\n![screenshot from 2015-04-01 16 43 56](https://cloud.githubusercontent.com/assets/163582/6953430/eb6a773a-d88e-11e4-9adf-dd0808115647.png)\r\n\r\n```r\r\nsregion = state.region[match(snames, tolower(state.name))]\r\npal = colorFactor('Dark2', sregion)\r\nleaflet(data = mapState) %>% addTiles() %>%\r\n  addPolygons(fillColor = pal(sregion), stroke = FALSE, fillOpacity = 0.5) %>%\r\n  addLegend(pal = pal, values = sregion)\r\n```\r\n\r\n![screenshot from 2015-04-01 16 44 16](https://cloud.githubusercontent.com/assets/163582/6953431/eb6ae328-d88e-11e4-850c-0530af70bc19.png)\r\n"},"comment":{"url":"https://api.github.com/repos/rstudio/leaflet/issues/comments/88663409","html_url":"https://github.com/rstudio/leaflet/pull/53#issuecomment-88663409","issue_url":"https://api.github.com/repos/rstudio/leaflet/issues/53","id":88663409,"user":{"login":"jcheng5","id":129551,"avatar_url":"https://avatars.githubusercontent.com/u/129551?v=3","gravatar_id":"","url":"https://api.github.com/users/jcheng5","html_url":"https://github.com/jcheng5","followers_url":"https://api.github.com/users/jcheng5/followers","following_url":"https://api.github.com/users/jcheng5/following{/other_user}","gists_url":"https://api.github.com/users/jcheng5/gists{/gist_id}","starred_url":"https://api.github.com/users/jcheng5/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jcheng5/subscriptions","organizations_url":"https://api.github.com/users/jcheng5/orgs","repos_url":"https://api.github.com/users/jcheng5/repos","events_url":"https://api.github.com/users/jcheng5/events{/privacy}","received_events_url":"https://api.github.com/users/jcheng5/received_events","type":"User","site_admin":false},"created_at":"2015-04-01T23:32:14Z","updated_at":"2015-04-01T23:32:14Z","body":"cc @hadley @wch "}}