r - 使用 R 中的 leaflet 按颜色区分分类变量

标签 r leaflet

我正在使用 R 中的“传单”制作华盛顿地区人口的交互式 map ,并且我想使用不同的颜色来区分 7 个不同的社区。我的变量是:地址、社区、性别和名称(用于弹出窗口)。我使用了 TrendCt 中的一些代码和 Ripples .

我如何调整我的代码以显示不同社区的不同颜色,甚至如何更改色调以区分社区中的性别?

这是我的代码:

###########################################################################
#################### D.C. Community Map in R ##############################
###########################################################################

## Retrieve Data and Download Package
# Use import data set dropdown menu to import data correctly
# Community Map.csv 

# Load packages
library(dplyr)
library(ggmap)
library(leaflet)

# Define new data set
ysa <- Community.Map

## Generate dataset with coordinate variables 
## Averages 10 minutes to render on my i5 processor
ysa %>% 
  mutate(address=paste(gender, sep=", ", ward)) %>%
  select(address) %>% 
  lapply(function(x){geocode(x, output="latlon")})  %>% 
  as.data.frame %>% 
  cbind(ysa) -> ysa1

ysa %>%  
  mutate(popup_info=paste(sep = "<br/>", paste0("<b>","<i>", ward,"<i>", "      
  </b>"), name)) %>% 
  mutate(lon=ifelse(is.na(longitude), address.lon, longitude),
         lat=ifelse(is.na(latitude),  address.lat, latitude)) %>%
  filter(!is.na(lon) & !grepl("CLOSED", ward)) -> ysa2

# Plot the map
leaflet(ysa2) %>%
  addProviderTiles("CartoDB.Positron") %>%
  addCircleMarkers(lng = ~longitude, 
                   lat = ~latitude, 
                   radius = 1.5, 
                   color = "red",
                   stroke=FALSE,
                   fillOpacity = 0.8,
                   popup = ~popup_info) %>%
addLegend("bottomright", colors= "red", labels="Community ", title="YSA     
Bounderies by Community")

我一直在尝试使用以下代码按颜色划分:

# color <- colorFactor(c("blue", "red", "green", "yellow", "brown", "gold", "purple"),    
domain = c("Braddock", "Shenandoah", "Langley", "DC 2nd", "Colonial 1st", 
"Colonial 2nd", "Glenn Dale"))

本质上,我想为每个社区分配不同的颜色,就像上面的文本中所尝试的那样,但我遗漏了一些东西。如果您有想法,请分享。

最佳答案

您似乎尚未将创建的调色板连接到数据和图例。尝试以下方法:

library(viridis) # My favorite palette for maps
wardpal <- colorFactor(viridis(7), ysa2$ward) # I'm assuming the variable ward contains the names of the communities.

leaflet(ysa2) %>%
    addProviderTiles("CartoDB.Positron") %>%
    addCircleMarkers(lng = ~longitude, 
                       lat = ~latitude, 
                       radius = 1.5, 
                       fillColor = ~wardpal(ward),
                       stroke=FALSE,
                       fillOpacity = 0.8,
                       popup = ~popup_info) %>%
    addLegend("bottomright", pal = wardpal, values = ~ward, labels = "Community ", title = "YSA Bounderies by Community")

您还想在颜色阴影中编码性别。您是否尝试过将 fillOpacity 映射到描述性别分布的变量(例如,fillOpacity = ~percent_female)。或者,您可以为每个社区添加一个单独的图层,并根据性别流行程度为每个图层设置调色板。您可以使用每个 addCirlayer 调用中的“group”参数将所有图层绑定(bind)在一起。

关于r - 使用 R 中的 leaflet 按颜色区分分类变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41310344/

相关文章:

r - 直接标签 : avoid clipping (like xpd=TRUE)

linux - cron 无法运行 R 脚本

r - Shiny 传单多边形的自定义静态着色

javascript - 使用JSON每隔1秒更新一次传单标记位置

javascript - 通过原型(prototype)继承对象

R日期内部整数存储有 "L"- 可以删除吗?

R 将语料库分解为句子

r - 将插槽的缺失值设置为默认值

javascript - 传单嘈杂的控制台输出

typescript - 如何为 Leaflet 插件添加 Typescript 定义