I have a leaflet map in which I have used addCircles, which are sized based on the population size of my data locations. I now want to color those circles based on the income of the population using colorNumeric. How do I use the population variable to determine my circle radii and simultaneously use the income variable to determine my color?
```{r}
library(leaflet)
leaflet()%>%
addTiles() %>%
addCircles(data = censusdata3, lng = ~Lon, lat = ~Lat, weight = 1, radius = ~households_estimate_total, popup = ~Geography, group = "Population", color)
```
Data sample:
https://docs.google.com/spreadsheets/d/1Kw2daQk5ur-A3HbdJt7K7krFZ9Uh9Xg726sFRlQXIUM/edit?usp=sharing
I basically just combined examples from two
leaflet
tutorial pages: https://rstudio.github.io/leaflet/colors.html and https://rstudio.github.io/leaflet/shapes.htmlYou can build a color palette with
colorNumeric
or one of the other palette functions inleaflet
, and then color the circles the same way you would for a choropleth.The csv file is just what's downloaded from your spreadsheet.
Not to editorialize, but I'd also suggest scaling the radius by the square root of the population, not the population itself, because people perceive the difference in area of circles. The example for shapes includes a similar map where they use
radius = ~sqrt(Pop) * 30
.