【发布时间】:2026-01-05 13:40:01
【问题描述】:
我想使用 ggplot2 (v.9) 绘制一张世界地图,它结合了两条 if 信息。以下示例说明:
library(rgdal)
library(ggplot2)
library(maptools)
# Data from http://thematicmapping.org/downloads/world_borders.php.
# Direct link: http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip
# Unpack and put the files in a dir 'data'
gpclibPermit()
world.map <- readOGR(dsn="data", layer="TM_WORLD_BORDERS_SIMPL-0.3")
world.ggmap <- fortify(world.map, region = "NAME")
n <- length(unique(world.ggmap$id))
df <- data.frame(id = unique(world.ggmap$id),
growth = 4*runif(n),
category = factor(sample(1:5, n, replace=T)))
## noise
df[c(sample(1:100,40)),c("growth", "category")] <- NA
ggplot(df, aes(map_id = id)) +
geom_map(aes(fill = growth, color = category), map =world.ggmap) +
expand_limits(x = world.ggmap$long, y = world.ggmap$lat) +
scale_fill_gradient(low = "red", high = "blue", guide = "colorbar")
但是,此解决方案不是同时显示 growth 和 category 的好方法。 Growth 高度可见,但 category 几乎看不到,因为它只是一个边框。
我试图增加边框的大小,但没有运气(新的 geom_map 很难使用)。有谁知道如何在上面的例子中增加边框大小,或者更好,一种显示两个因素的机制?
一个额外的问题:国家名称,例如地图包(其中包含苏联!)使用的国家名称是示例中使用的数据是脆弱的。我更喜欢使用 ISO 3166-1 alpha-3(1)。有谁知道 ggplot2 可以轻松使用的数据,其中包含 ISO-...国家名称(包含在链接数据中)
结果:
【问题讨论】:
-
world.map 是什么?它没有在您的代码中定义。如果我尝试 fortify(w,region="NAME"),我会收到“无效的多字节字符”错误。请提供可重现的代码。
-
我在 fortify 行收到以下错误:“nchar(ID) 中的错误:无效的多字节字符串 1”
-
我无法在 UTF8 系统上重现;我怀疑字体编码在这里是个问题。 iconv(.) 可以从一种编码系统转换为另一种编码系统。
标签: r ggplot2 geospatial