在 R 中的传单包制作的我的地图中插入簇颜色的一般功能
General function to insert the colors of the clusters in my map made by the leaflet package in R
我如何做一个通用函数来插入簇的颜色?我使用传单包来生成地图。我这样做的方法是 "if else",它奏效了,但是如果我有 15 个集群,我会有很多 "if else"。有谁能够帮助我 ??此外,如果可能的话,我也想把星团的图例放在我的地图上。我的可执行代码如下:
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()
可以很容易地通过另一个数字向量进行采样或索引:
> colors()[c(1,4,5,6,9)]
[1] "white" "antiquewhite1" "antiquewhite2" "antiquewhite3" "aquamarine1"
但是leaflet::awesomeIcons
只支持某些看起来不错的颜色。您可以从 ?awesomeIcons
:
获取此列表
markerColor
Possible values are "red", "darkred", "lightred", "orange", "beige", "green", "darkgreen", "lightgreen", "blue", "darkblue", "lightblue", "purple", "darkpurple", "pink", "cadetblue", "white", "gray", "lightgray", "black"
所以我们可以将它们放在一个向量中并使用簇列对它们进行索引:
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
中允许的颜色数量,这就会起作用。
完整代码:
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
编辑:在一个图例中添加两组点
我们可以将第二个数据集的点与第一个数据集的点结合起来,并将它们绘制在一起。然后加上图例,一切就都在一起了。
我们可以为第二组点添加一个簇号 19。这将对应于 awesomeIcons
颜色集中的最后一种颜色。 (您可以将其设置为任何值,但请记住簇的数量与可用颜色的数量。)
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
我如何做一个通用函数来插入簇的颜色?我使用传单包来生成地图。我这样做的方法是 "if else",它奏效了,但是如果我有 15 个集群,我会有很多 "if else"。有谁能够帮助我 ??此外,如果可能的话,我也想把星团的图例放在我的地图上。我的可执行代码如下:
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()
可以很容易地通过另一个数字向量进行采样或索引:
> colors()[c(1,4,5,6,9)]
[1] "white" "antiquewhite1" "antiquewhite2" "antiquewhite3" "aquamarine1"
但是leaflet::awesomeIcons
只支持某些看起来不错的颜色。您可以从 ?awesomeIcons
:
markerColor
Possible values are "red", "darkred", "lightred", "orange", "beige", "green", "darkgreen", "lightgreen", "blue", "darkblue", "lightblue", "purple", "darkpurple", "pink", "cadetblue", "white", "gray", "lightgray", "black"
所以我们可以将它们放在一个向量中并使用簇列对它们进行索引:
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
中允许的颜色数量,这就会起作用。
完整代码:
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
编辑:在一个图例中添加两组点
我们可以将第二个数据集的点与第一个数据集的点结合起来,并将它们绘制在一起。然后加上图例,一切就都在一起了。
我们可以为第二组点添加一个簇号 19。这将对应于 awesomeIcons
颜色集中的最后一种颜色。 (您可以将其设置为任何值,但请记住簇的数量与可用颜色的数量。)
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