gpt4 book ai didi

r:带有自定义图例和永久显示国名的 Choropleth

转载 作者:行者123 更新时间:2023-12-01 11:25:34 24 4
gpt4 key购买 nike

以下代码根据层次聚类创建 Choropleth map (使用 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")

现在我想改变两件事:
  • 常驻 显示国家名称,即不仅在将鼠标悬停在 RStudio 中的交互式图表上时。我试图应用 this post 的答案到我的,没有成功。
  • 非连续刻度 .理想情况下,我希望一个集群完全没有规模但 5 个盒子,每个盒子具有以下三个特征:
    (1) 方框的颜色应根据其在等值线中的颜色而定
    (2) 每个方框应包含分配给它的国家数量,即分配给各自的集群
    (3) 每个框应包含cluster.means 中包含的两个变量(X1, X2) 各自的聚类均值。

  • 我附上了这样一个我所想的示例图(它不必看起来完全相同 - 只是为了传达这个想法)。

    An exemplary choroplot containing only 2, instead of 5 boxes

    任何帮助、建议、tipp 都非常感谢!

    (缩放)数据如下所示:
    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")

    最佳答案

    “理想情况下,我希望完全没有规模” :这是通过添加参数 showscale=F 来实现的至plot_ly() .

    “永久显示国名” :这是通过添加 scattergeo 来完成的绘图层,如 this post 中所述, 略有不同。因为您的数据框包含一列国家名称,但不是国家代码之一,您需要添加参数 locationmode = 'country names'

    p <- plot_ly(df, z = member, type = 'choropleth', 
    locations = Country, locationmode = 'country names',
    text = Country, hoverinfo = "text",
    showscale=F, inherit =F) %>%
    layout(geo = g, title = "Energy markets in Africa") %>%
    add_trace(type="scattergeo",
    locationmode = 'country names', locations = Country,
    text = Country, mode="text",
    textfont = list(color=rgb(1,0.5,0.3), size =12))

    您将需要尝试各种配色方案,以找到一种能有效显示集群的配色方案,同时使永久国家名称标签相对可见。本地图缩小时,永久国家名称标签将相互重叠,您可能无能为力。通过额外的努力,您可以为每个标签指定一个经纬度位置,而不是依赖于它们的默认位置,这可能有助于通过更好地分离标签来改善 map 的外观。不过,我认为,由于您要求提供永久标签,因此您打算仅以合适的缩放比例使用 map ,使它们看起来不错。

    添加文本框您描述的,您可以使用注释布局(每个集群一个注释)。我在这里展示了如何添加一个带有紫色边框和第二个绿松石色的边框。您需要将其调整为最终使用的配色方案。
    box1 <- list(
    x = 0.3,
    y = 0.5,
    yanchor = "top",
    borderpad = 2,
    bordercolor = rgb(0.5,0.1,0.5), # set this same as color of cluster 1
    borderwidth = 5,
    text = paste("1. Cluster, ",
    sum(df$member==1), # number of countries in cluster
    " Countries<br>X1 = ", # use <br> for line breaks
    format(round(cluster.means[1,]$X1, 2), nsmall = 2),
    "<br>X2 = ",
    format(round(cluster.means[1,]$X2, 2), nsmall = 2)),
    align = "left",
    showarrow = F)

    box2 <- list(
    x = 0.3,
    y = 0.4,
    yanchor = "top",
    borderpad = 2,
    bordercolor = rgb(0.1,0.5,0.5),
    borderwidth = 5,
    text = paste("2. Cluster, ",
    sum(df$member==2),
    " Countries<br>X1 = ",
    format(round(cluster.means[2,]$X1, 2), nsmall = 2),
    "<br>X2 = ",
    format(round(cluster.means[2,]$X2, 2), nsmall = 2)),
    align = "left",
    showarrow = F)

    p %>% layout(annotations = list(box1, box2))

    enter image description here

    关于r:带有自定义图例和永久显示国名的 Choropleth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37529739/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com