通过R中的传单包在我的地图中插入簇的颜色的常规功能 [英] General function to insert the colors of the clusters in my map made by the leaflet package in R

查看:84
本文介绍了通过R中的传单包在我的地图中插入簇的颜色的常规功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何执行一般功能以插入群集的颜色?我使用了传单包来生成地图。我这样做的方式是 if else,它可以工作,但是例如,如果我有15个群集,那么我将有很多 if else。有谁能够帮助我 ??另外,如果可能的话,我也想在我的地图上放置这些聚类的图例。我的可执行代码如下:

 库(传单)
库(地球)

#database
df< -structure(list(Properties = c(1,2,3,4,5,6,7,8,9,10),纬度= c(-23.2,-23.6,-23.9 ,-23.9,-23.6,-23.5,-23.9,-23.9,-23.6,-23.9),
经度= 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

#使用传单

example = df
getColor<-function(example){
sapply (example $ cluster,function(cluster){
if(cluster == 1){
blue
} else if(cluster == 2){
green
} else if(cluster == 3){
橙色
} else {
红色
}})
}

icons<-awesomeIcons(
icon ='ios-close',
iconColor ='黑色',
库='ion',
markerColor = getColor(示例)


m =传单(示例)%> %addTiles()%>%
addAwesomeMarkers(lat =〜Latitude,lng =〜Longitude,icon = icons,label =〜as.character(cluster))
m

解决方案

一种为聚类分配颜色的好简单方法是简单地用聚类向量索引颜色向量。在 R 中,可以将颜色指定为名称(白色,红色)或数字。并且有一个内置函数?colors(),可以轻松地通过另一个数字矢量进行采样或索引:

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

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


markerColor

可能的值为 red, darkred, lightred, orange, beige, green, darkgreen, lightgreen, blue, darkblue, lightblue,紫色,暗紫色,粉红色, cadetblue,白色,灰色,浅灰色,黑色


因此,我们可以将它们放入向量中并使用簇列对其进行索引:

  ai_cols<-c(红色,深红色,浅红色,橙色,米色,绿色,深绿色,浅绿色,蓝色,深蓝色,浅蓝色,紫色,深紫色 , pink, cadetblue, white, gray, lightgray, black)
ai_cols [example $ cluster]
[1]红色红色暗红色 darkred lightred lightred orange orange orange orange

只要簇的数量小于或等于 awesomeIcons






完整代码:

 库(传单)
库(地球)

#数据库
df<-
结构(
list(
属性= c(1、2、3、4、5、6、7、8、9、10),
纬度= c(
-23.2,
-23.6 ,
-23.9,
-23.9,
-23.6,
-23.5,
-23.9,
-23.9,
-23.6,
-23.9
),
经度= 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<-群集

#使用传单

的示例= df
ai_colors<-
c(
red,
darkred,
浅红色,
橙色,
米色,
绿色,
深绿色,
浅绿色,
蓝色 ,
深蓝色,
浅蓝色,
紫色,
黑暗紫色,
粉红色,
cadetblue,
白色,
灰色,
浅灰色,
黑色
),

clust_colors<-ai_colors [ example $ cluster]

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


m =传单(示例)%>%addTiles()%&%;%
addAwesomeMarkers(
lat =〜纬度,
lng =〜经度,
icon =图标,
标签=〜as.character(cluster)

m






编辑:在一个图例中添加两组点



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



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

  df1< ;-
结构(
list(
属性= c(1,2,3,4,5),
纬度= c(-23.8,-23.4,-23.2, -23.7,-23.8),
经度= 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(例如,df1 )

然后像以前一样绘制:

  clust_colors<-ai_colors [all_points $ cluster] 

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


m = leaflet(all_points)%>%addTiles()%> %
addAwesomeMarkers(
lat =〜纬度,
lng =〜经度,
icon =图标,
标签=〜as.character(all_points $ cluster)
)%>%
addLegend(
position = topright,
title = Cluster,
colors = ai_colors [unique(all_points $ cluster)],
标签=唯一(all_points $ cluster)

m


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

Thank you very much!!

Insert addLegend

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

Image as example:

解决方案

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" 

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" 

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


Full code:

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


Edit: add two sets of points in one legend

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.

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)

Then plot as before:

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天全站免登陆