r:根据使用 cutree() 的层次聚类的等值线图
r: Choropleth map according to hierarchial clustering using cutree()
我正在对 50 个不同的非洲国家/地区的两个参数 (X1, X2) 进行层次聚类。因此,我想在非洲大陆内识别出 5 个不同的 groups/clusters。我正在使用以下代码来执行此操作:
hc <- hclust(dist(df), method = "complete")
member <- cutree(hc, 5)
现在,我想使用 member
中存储的信息(即每个国家所属的集群 ID)为非洲地图着色,以便每个集群都用另一种颜色表示。我知道有很多关于着色地图的教程,比如this。但我想知道是否有针对层次聚类分析结果的特定地图着色方法?
有人做过吗?我很高兴就如何以最有效的方式实现这一目标提出任何建议或建议!
(缩放)数据如下所示:
df <- structure(list(Country = structure(1:50, .Label = c("Angola",
"Benin", "Botswana", "Burkina Faso", "Burundi", "Cabo Verde",
"Cameroon", "Central African Republic", "Chad", "Comoros", "Congo",
"Cote d'Ivoire", "Democratic Republic of Congo", "Djibouti",
"Equatorial Guinea", "Eritrea", "Ethiopia", "Gabon", "Gambia",
"Ghana", "Guinea", "Guinea-Bissau", "Kenya", "Lesotho", "Liberia",
"Madagascar", "Malawi", "Mali", "Mauritania", "Mauritius", "Mozambique",
"Namibia", "Niger", "Nigeria", "Reunion", "Rwanda", "Sao Tome and Principe",
"Senegal", "Seychelles", "Sierra Leone", "Somalia", "South Africa",
"South Sudan", "Sudan", "Swaziland", "Tanzania", "Togo", "Uganda",
"Zambia", "Zimbabwe"), class = "factor"), X1 = c(-0.18, -1.03,
0.6, 1.55, 0.22, 0.26, 0.76, 2.15, -1.43, 0.99, 1.79, -0.39,
1.73, 1.57, 1.11, -0.09, -1.49, -0.46, -0.48, -1.22, -0.78, -1.46,
-1.22, 0.35, 0.45, 1.29, -1.37, -0.61, 0.92, -1.3, 0.42, -1.18,
1.4, -0.83, 0.06, -0.76, -0.19, -0.37, -0.63, 0.64, 0.93, 0.33,
-0.76, -0.21, -0.59, -0.41, -0.74, 0.39, -1.1, 1.35), X2 = c(-0.22,
-0.42, 0.72, -0.59, -1.27, 0.64, -1.35, -1.4, -0.35, -1.43, 1.07,
-0.01, -0.51, 0.11, 1.14, -0.89, 0.77, 1.45, -1.67, -0.83, 0.71,
0.92, 1.63, 1.68, 0.23, -0.18, 0.07, 0.8, -0.02, 0.82, -0.72,
-0.41, -0.26, 0.02, -1.68, 1.67, 0.18, 0.98, 1.45, 0.31, -1.23,
-1.38, -0.63, 1.41, -0.12, 0, -1.3, -1.64, 0.21, 1.52)), .Names = c("Country",
"X1", "X2"), row.names = c(NA, -50L), class = "data.frame")
使用您的数据并绘制:
library(plotly)
hc <- hclust(dist(df), method = "complete")
df$member <- cutree(hc, 5)
#Grabbing the Africa Geo from a plotly example
g <- list(
scope = 'africa',
showframe = F,
showland = T,
landcolor = toRGB("grey90")
)
plot_ly(df, z = member, type = 'choropleth', mode = 'markers', locations = Country,
locationmode = 'country names') %>% layout(geo = g)
您可以使用此处的绘图设置:https://plot.ly/r/reference/
切换到非连续比例尺,但碰巧连续比例尺仅适用于绘制 5。要获得更多,您需要发散色标尺。
我正在对 50 个不同的非洲国家/地区的两个参数 (X1, X2) 进行层次聚类。因此,我想在非洲大陆内识别出 5 个不同的 groups/clusters。我正在使用以下代码来执行此操作:
hc <- hclust(dist(df), method = "complete")
member <- cutree(hc, 5)
现在,我想使用 member
中存储的信息(即每个国家所属的集群 ID)为非洲地图着色,以便每个集群都用另一种颜色表示。我知道有很多关于着色地图的教程,比如this。但我想知道是否有针对层次聚类分析结果的特定地图着色方法?
有人做过吗?我很高兴就如何以最有效的方式实现这一目标提出任何建议或建议!
(缩放)数据如下所示:
df <- structure(list(Country = structure(1:50, .Label = c("Angola",
"Benin", "Botswana", "Burkina Faso", "Burundi", "Cabo Verde",
"Cameroon", "Central African Republic", "Chad", "Comoros", "Congo",
"Cote d'Ivoire", "Democratic Republic of Congo", "Djibouti",
"Equatorial Guinea", "Eritrea", "Ethiopia", "Gabon", "Gambia",
"Ghana", "Guinea", "Guinea-Bissau", "Kenya", "Lesotho", "Liberia",
"Madagascar", "Malawi", "Mali", "Mauritania", "Mauritius", "Mozambique",
"Namibia", "Niger", "Nigeria", "Reunion", "Rwanda", "Sao Tome and Principe",
"Senegal", "Seychelles", "Sierra Leone", "Somalia", "South Africa",
"South Sudan", "Sudan", "Swaziland", "Tanzania", "Togo", "Uganda",
"Zambia", "Zimbabwe"), class = "factor"), X1 = c(-0.18, -1.03,
0.6, 1.55, 0.22, 0.26, 0.76, 2.15, -1.43, 0.99, 1.79, -0.39,
1.73, 1.57, 1.11, -0.09, -1.49, -0.46, -0.48, -1.22, -0.78, -1.46,
-1.22, 0.35, 0.45, 1.29, -1.37, -0.61, 0.92, -1.3, 0.42, -1.18,
1.4, -0.83, 0.06, -0.76, -0.19, -0.37, -0.63, 0.64, 0.93, 0.33,
-0.76, -0.21, -0.59, -0.41, -0.74, 0.39, -1.1, 1.35), X2 = c(-0.22,
-0.42, 0.72, -0.59, -1.27, 0.64, -1.35, -1.4, -0.35, -1.43, 1.07,
-0.01, -0.51, 0.11, 1.14, -0.89, 0.77, 1.45, -1.67, -0.83, 0.71,
0.92, 1.63, 1.68, 0.23, -0.18, 0.07, 0.8, -0.02, 0.82, -0.72,
-0.41, -0.26, 0.02, -1.68, 1.67, 0.18, 0.98, 1.45, 0.31, -1.23,
-1.38, -0.63, 1.41, -0.12, 0, -1.3, -1.64, 0.21, 1.52)), .Names = c("Country",
"X1", "X2"), row.names = c(NA, -50L), class = "data.frame")
使用您的数据并绘制:
library(plotly)
hc <- hclust(dist(df), method = "complete")
df$member <- cutree(hc, 5)
#Grabbing the Africa Geo from a plotly example
g <- list(
scope = 'africa',
showframe = F,
showland = T,
landcolor = toRGB("grey90")
)
plot_ly(df, z = member, type = 'choropleth', mode = 'markers', locations = Country,
locationmode = 'country names') %>% layout(geo = g)
您可以使用此处的绘图设置:https://plot.ly/r/reference/
切换到非连续比例尺,但碰巧连续比例尺仅适用于绘制 5。要获得更多,您需要发散色标尺。