gpt4 book ai didi

r - 带 l 的 ggplot map

转载 作者:行者123 更新时间:2023-12-02 03:58:39 25 4
gpt4 key购买 nike

我想使用 ggplot2 (v.9) 绘制世界地图,它结合了两部分信息。以下示例说明:

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 几乎不可能看到,因为它只是一个边界。

我尝试增加边框的大小,但没有成功(新的 geom_map 很难使用)。有谁知道如何在上面的示例中增加边框大小,或者更好的是,显示两个因素的机制?

一个额外的问题:国家名称,例如 map 包使用的国家名称(以苏联为特色!),示例中使用的数据是脆弱的。我更喜欢使用 ISO 3166-1 alpha-3( 1 )。有谁知道可以与 ggplot2 轻松使用的数据,其中包含 ISO-...国家/地区名称(包含在链接数据中)

结果:

result http://ompldr.org/vY3hsYQ

最佳答案

我会使用不同的色调范围来填充和线条颜色:

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(high = "red", low = "white", guide = "colorbar") +
scale_colour_hue(h = c(120, 240))

enter image description here

或者,使用填充作为类别,使用透明度作为增长水平。

ggplot(df, aes(map_id = id)) +
geom_map(aes(alpha = growth, fill = category), map =world.ggmap) +
expand_limits(x = world.ggmap$long, y = world.ggmap$lat) +
scale_alpha(range = c(0.2, 1), na.value = 1)

enter image description here

这取决于您想要展示的内容。

以防万一,以下是更改线条大小的方法:

ggplot(df, aes(map_id = id)) +
geom_map(aes(fill = growth, color = category, size = factor(1)), map =world.ggmap) +
expand_limits(x = world.ggmap$long, y = world.ggmap$lat) +
scale_fill_gradient(high = "red", low = "white", guide = "colorbar") +
scale_colour_hue(h = c(120, 240)) +
scale_size_manual(values = 2, guide = FALSE)

enter image description here

这是 HSV 版本:

df$hue <- ifelse(is.na(df$category), 0, as.numeric(df$category)/max(as.numeric(df$category), na.rm=T))
df$sat <- ifelse(is.na(df$growth), 0, df$growth/max(df$growth, na.rm=T))
df$fill <- ifelse(is.na(df$category), "grey50", hsv(df$hue, df$sat))

ggplot(df, aes(map_id = id)) +
geom_map(aes(fill = fill), map =world.ggmap) +
expand_limits(x = world.ggmap$long, y = world.ggmap$lat) +
scale_fill_identity(guide = "none")

enter image description here

关于r - 带 l 的 ggplot map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9558040/

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