在 R 中的传单包制作的地图中插入集群颜色的通用函数 [英] General function to insert the colors of the clusters in my map made by the leaflet package in R

查看:20
本文介绍了在 R 中的传单包制作的地图中插入集群颜色的通用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何执行一般功能来插入簇的颜色?我使用传单包来生成地图.我这样做的方式是if else",它起作用了,但是如果例如,我有 15 个集群,我将有很多if else".有谁能够帮我 ??此外,如果可能的话,我也想在我的地图上放上集群的图例.我的可执行代码如下:

How I do a general function to insert the colors of the clusters? I used the leaflet package to generate the map. The way I did it was "if else", it worked, but if for example, I have 15 clusters, I will have many "if else". Can anybody help me ?? In addition, if possible I would like to put legend of the clusters on my map as well. My executable code is below:

library(leaflet)
library(geosphere)

#database
df<-structure(list(Properties = c(1,2,3,4,5,6,7,8,9,10), Latitude = c(-23.2, -23.6, -23.9, -23.9, -23.6,  -23.5, -23.9, -23.9, -23.6, -23.9), 
Longitude = c(-49.6, -49.6, -49.6, -49.4, -49.3, -49.9, -49.3, -49.2, -49.6, -49.9)), class="data.frame",row.names = c(NA, -10L))

#clusters
d<-as.dist(distm(df[,2:1]))
fit.average<-hclust(d,method="average") 
clusters<-cutree(fit.average, 4) 
df$cluster<-clusters

#Map using leaflet

example=df
getColor <- function(example) {
  sapply(example$cluster, function(cluster) {
    if(cluster == 1) {
      "blue"
    } else if(cluster == 2) {
      "green"
    } else if(cluster == 3) {
      "orange"
    } else {
      "red"
    } })
}

icons <- awesomeIcons(
  icon = 'ios-close',
  iconColor = 'black',
  library = 'ion',
  markerColor = getColor(example)
)

m=leaflet(example) %>% addTiles() %>%
  addAwesomeMarkers(lat=~Latitude, lng = ~Longitude, icon=icons, label=~as.character(cluster))
m

非常感谢!!

插入添加图例

df1<-structure(list(Properties = c(1,2,3,4,5), Latitude = c(-23.8, -23.4, -23.2, -23.7,-23.8), 
Longitude = c(-49.9, -49.2, -49.3, -49.1,-49.9)), class="data.frame",row.names = c(NA, -5L))

m = leaflet(example) %>% addTiles() %>%
  addAwesomeMarkers(lat =  ~ Latitude,lng = ~ Longitude,icon = icons,label =  ~ as.character(cluster)) %>% 
addLegend( position = "topright", title="Cluster", colors = ai_colors[1:max(df$cluster)],labels = unique(df$cluster))%>%
addAwesomeMarkers(leaflet(df1) %>% addTiles(), lat=~df1$Latitude, lng = ~df1$Longitude)
m

图片为例:

推荐答案

将颜色分配给集群的一种好且简单的方法是通过集群向量简单地索引颜色向量.在 R 中,颜色可以指定为名称('white', 'red')或数字.还有一个内置函数 ?colors() 可以很容易地通过另一个数字向量进行采样或索引:

One good and simple way to assign colors to clusters is to simply index a vector of colors by a vector of clusters. In R colors can be specified as names ('white', 'red') or as numbers. And there is a built in function ?colors() that makes it easy to sample or index by another numeric vector:

> colors()[c(1,4,5,6,9)]
[1] "white"         "antiquewhite1" "antiquewhite2" "antiquewhite3" "aquamarine1" 

但是 leaflet::awesomeIcons 只支持某些看起来不错的颜色.您可以从 ?awesomeIcons 获取此列表:

But leaflet::awesomeIcons only supports certain colors that look pretty good. You can get this list from ?awesomeIcons:

标记颜色
可能的值是红色"、深红色"、浅红色"、橙色"、米色"、绿色"、深绿色"、浅绿色"、蓝色"、深蓝色"、浅蓝色"、紫色", 深紫色", 粉色", 军校蓝", 白色", 灰色", 浅灰色", 黑色"

markerColor
Possible values are "red", "darkred", "lightred", "orange", "beige", "green", "darkgreen", "lightgreen", "blue", "darkblue", "lightblue", "purple", "darkpurple", "pink", "cadetblue", "white", "gray", "lightgray", "black"

所以我们可以把它们放在一个向量中,并用簇列索引它们:

So we can put these in a vector and index them with the cluster column:

ai_cols <- c("red", "darkred", "lightred", "orange", "beige", "green", "darkgreen", "lightgreen", "blue", "darkblue", "lightblue", "purple", "darkpurple", "pink", "cadetblue", "white", "gray", "lightgray", "black")
ai_cols[example$cluster]
[1] "red"      "red"      "darkred"  "darkred"  "lightred" "lightred" "orange"   "orange"   "orange"   "orange" 

只要集群的数量小于或等于 awesomeIcons 中允许的颜色数量,这将起作用.

This will work as long as the number of clusters is less than or equal to the number of colors allowed in awesomeIcons.

完整代码:

library(leaflet)
library(geosphere)

#database
df <-
  structure(
    list(
      Properties = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
      Latitude = c(
        -23.2,
        -23.6,
        -23.9,
        -23.9,
        -23.6,
        -23.5,
        -23.9,
        -23.9,
        -23.6,
        -23.9
      ),
      Longitude = c(
        -49.6,
        -49.6,
        -49.6,
        -49.4,
        -49.3,
        -49.9,
        -49.3,
        -49.2,
        -49.6,
        -49.9
      )
    ),
    class = "data.frame",
    row.names = c(NA,-10L)
  )

#clusters
d <- as.dist(distm(df[, 2:1]))
fit.average <- hclust(d, method = "average")
clusters <- cutree(fit.average, 4)
df$cluster <- clusters

#Map using leaflet

example = df
ai_colors <-
  c(
    "red",
    "darkred",
    "lightred",
    "orange",
    "beige",
    "green",
    "darkgreen",
    "lightgreen",
    "blue",
    "darkblue",
    "lightblue",
    "purple",
    "darkpurple",
    "pink",
    "cadetblue",
    "white",
    "gray",
    "lightgray",
    "black"
  )

clust_colors <- ai_colors[example$cluster]

icons <- awesomeIcons(
  icon = 'ios-close',
  iconColor = 'black',
  library = 'ion',
  markerColor = clust_colors
)

m = leaflet(example) %>% addTiles() %>%
  addAwesomeMarkers(
    lat =  ~ Latitude,
    lng = ~ Longitude,
    icon = icons,
    label =  ~ as.character(cluster)
  )
m

<小时>

在一个图例中添加两组点

我们可以将第二个数据集的点与第一个数据集结合起来,并将它们绘制在一起.然后当我们添加图例时,一切都会在一起.

We can combine the points of the second dataset with the first, and plot them together. Then when we add legend, everything is going to be together.

我们可以为第二组点添加一个 19 号簇.这将对应于 awesomeIcons 颜色集中的最后一种颜色.(您可以将其设置为任何值,但请记住集群的数量与可用颜色的数量.)

We can add a cluster number 19 for the second set of points. This would correspond to the last color in the awesomeIcons color set. (You can set this to anything, but keep in mind the number of clusters vs. number of available colors.)

df1 <-
  structure(
    list(
      Properties = c(1, 2, 3, 4, 5),
      Latitude = c(-23.8,-23.4,-23.2,-23.7, -23.8),
      Longitude = c(-49.9,-49.2,-49.3,-49.1, -49.9)
    ),
    class = "data.frame",
    row.names = c(NA,-5L)
  )


df1$cluster <- 19
all_points <- rbind(example, df1)

然后像以前一样绘制:

clust_colors <- ai_colors[all_points$cluster]

icons <- awesomeIcons(
  icon = 'ios-close',
  iconColor = 'black',
  library = 'ion',
  markerColor = clust_colors
)

m = leaflet(all_points) %>% addTiles() %>%
  addAwesomeMarkers(
    lat =  ~ Latitude,
    lng = ~ Longitude,
    icon = icons,
    label =  ~ as.character(all_points$cluster)
  ) %>%
  addLegend(
    position = "topright",
    title = "Cluster",
    colors = ai_colors[unique(all_points$cluster)],
    labels = unique(all_points$cluster)
  ) 
m

这篇关于在 R 中的传单包制作的地图中插入集群颜色的通用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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