如何在传单的addCircles中使用colorNumeric [英] how to use colorNumeric within addCircles in leaflet

查看:145
本文介绍了如何在传单的addCircles中使用colorNumeric的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个传单地图,其中使用了addCircles,其大小根据我的数据位置的总体大小而定.我现在想使用colorNumeric根据人口的收入为这些圆圈上色.如何使用种群变量来确定我的圆半径,同时如何使用收入变量来确定我的颜色?

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) 
```

数据样本:

https://docs.google.com/spreadsheetss /d/1Kw2daQk5ur-A3HbdJt7K7krFZ9Uh9Xg726sFRlQXIUM/edit?usp = sharing

推荐答案

我基本上只是结合了两个leaflet教程页面中的示例: https://rstudio.github.io/leaflet/shapes.html

I basically just combined examples from two leaflet tutorial pages: https://rstudio.github.io/leaflet/colors.html and https://rstudio.github.io/leaflet/shapes.html

您可以使用colorNumericleaflet中的其他调色板功能之一来构建调色板,然后以与制作圆度相同的方式为圆圈上色.

You can build a color palette with colorNumeric or one of the other palette functions in leaflet, and then color the circles the same way you would for a choropleth.

csv文件就是从电子表格中下载的文件.

The csv file is just what's downloaded from your spreadsheet.

library(tidyverse)
library(leaflet)

censusdata3 <- read_csv("censusdata - ACS_16_5YR_S1901_with_ann.csv") %>%
    setNames(c("Geography", "Lon", "Lat", "total", "median_income", "median_income_moe", "mean_income", "mean_income_moe")) %>%
    mutate_at(vars(total:mean_income_moe), as.numeric)

color_pal <- colorNumeric(palette = "magma", domain = censusdata3$median_income, reverse = F)

leaflet()%>%
    addTiles() %>%
    addCircles(data = censusdata3, 
                         lng = ~Lon, lat = ~Lat, 
                         weight = 1, 
                         radius = ~total, 
                         popup = ~Geography,
                         color = ~color_pal(median_income)
                         ) 

不做社论,但我还建议您按人口的平方根而不是人口本身的平方根来缩放半径,因为人们会感知圆的面积差异.形状的示例包括一个相似的地图,其中它们使用radius = ~sqrt(Pop) * 30.

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.

这篇关于如何在传单的addCircles中使用colorNumeric的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆