gpt4 book ai didi

r - 传单呈现不正确的国家

转载 作者:行者123 更新时间:2023-12-02 01:02:14 27 4
gpt4 key购买 nike

我正在尝试使用 leaflet 绘制几个国家/地区但它显示的国家是不正确的。以下是我的最小可重现示例:

library(leaflet)
library(maps)


df <- data.frame(name = c("Afghanistan", "Albania" , "Algeria" , "Armenia"),
code = c("AFG", "ALB", "DZA", "ARM"),
val = c(5, 10, 15, 20), stringsAsFactors = FALSE)

pal <- colorNumeric(
palette = "Blues",
domain = as.numeric(df$val))

labels <- sprintf(
"<strong>Country:%s</strong><br/>Value:%g /",
df$name, df$val)%>% lapply(htmltools::HTML)

Country = map("world", fill = TRUE, plot = FALSE, regions=iso.expand(df$code,regex = TRUE))

leaflet(Country) %>% addTiles() %>%
addPolygons(fillOpacity = 0.6, smoothFactor = 0.5, stroke = TRUE, weight = 1,
color = ~pal(as.numeric(df$val)),
label = labels)

我收到以下传单: enter image description here

如您所见, Algolia 显示为阿尔巴尼亚。如果我从我的数据中删除亚美尼亚并绘制传单,我会得到正确的位置。以下是代码和图像。
library(leaflet)
library(maps)

df <- data.frame(name = c("Afghanistan", "Albania" , "Algeria" ),
code = c("AFG", "ALB", "DZA"),
val = c(5, 10, 15), stringsAsFactors = FALSE)

pal <- colorNumeric(
palette = "Blues",
domain = as.numeric(df$val))

labels <- sprintf(
"<strong>Country:%s</strong><br/>Value:%g /",
df$name, df$val)%>% lapply(htmltools::HTML)

Country = map("world", fill = TRUE, plot = FALSE, regions=iso.expand(df$code,regex = TRUE))

leaflet(Country) %>% addTiles() %>%
addPolygons(fillOpacity = 0.6, smoothFactor = 0.5, stroke = TRUE, weight = 1,
color = ~pal(as.numeric(df$val)),
label = labels)

enter image description here

我错过了什么吗?

最佳答案

我认为问题与 map 中的多边形顺序有关对象(如果您设置 label = Country$names ,标签现在是正确的)。反正你可以通过转换Country来解决问题成SpatialPolygons对象(见 here)。

library(maptools)
IDs <- sapply(strsplit(Country$names, ":"), function(x) x[1])
Country <- map2SpatialPolygons(Country,
IDs=IDs,
proj4string=CRS("+proj=longlat +datum=WGS84"))

leaflet(Country) %>% addTiles() %>%
addPolygons(fillOpacity = 0.6, smoothFactor = 0.5, stroke = TRUE, weight = 1,
color = pal(as.numeric(df$val)),
label = labels)

enter image description here

关于r - 传单呈现不正确的国家,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49798198/

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