4

次のコードは、階層クラスタリングに従って ( と を使用hclust()してcutree())コロプレス マップを作成します。

library(plotly)
library(cluster)

hc <- hclust(dist(df), method = "complete")
df$member <- cutree(hc, 5)

cluster.means = (as.data.frame(aggregate(df[,-1], list(cluster= df$member), mean)))[,-4]

g = list(
  scope = 'africa',
  showframe = T,
  showland = T,
  landcolor = toRGB("white")
)

plot_ly(df, z = member, type = 'choropleth', locations = Country,
              locationmode = 'country names', text = Country, hoverinfo = "text") %>% 
        layout(geo = g, title = "Energy markets in Africa")

ここで、次の 2 つの点を変更したいと思います。

  1. 国名の永続的な表示。つまり、RStudio の対話型チャートにカーソルを合わせたときだけではありません。この投稿の答えを私のものに適用しようとしましたが、成功しませんでした。
  2. 非連続音階。理想的には、縮尺はまったく必要ありませんが、それぞれ次の 3 つの特徴を持つ 1 つのクラスターに 5 つのボックスを使用したいと考えています: (1) ボックスの色は、コロプレスのそれぞれの色に従う必要があります。 (3) 各ボックスには、2 つの変数 (X1、X2) のそれぞれのクラスター平均が含まれている必要があります。cluster.means

私が考えていることのそのような模範的なプロットを添付しました (まったく同じに見える必要はありません - アイデアを伝えるためだけに)。

5 つのボックスではなく 2 つのボックスのみを含む模範的なコロプロット

ヘルプ、アドバイス、ヒントは大歓迎です!

(スケーリングされた) データは次のようになります。

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")
4

1 に答える 1