gpt4 book ai didi

r - 在世界地图上叠加多个河图(桑基图)

转载 作者:行者123 更新时间:2023-12-04 11:16:52 25 4
gpt4 key购买 nike

下面的迷人图表位于《经济学人》2016 年 1 月 30 日第 61 页。它描绘了从五个地区到六个地区的液化天然气 (LNG) 导出情况。 R 怎么能画出与之相似的东西,也许有几个桑基图(来自包 riverplots),但箭头与所描绘的箭头相反?这意味着,流入导出地区的流量将显示为流出。

Economist map with flows

通过眼球从图中提取数据产生 df 数据框。变量有奇怪的名称,因为 `riverplot 需要唯一的节点名称。

> dput(df)
structure(list(ID = structure(c(1L, 6L, 9L, 13L, 2L, 7L, 14L,
3L, 10L, 15L, 4L, 11L, 5L, 8L, 12L, 16L), .Label = c("Africa-Asia",
"Africa-Europe", "Africa-Nam", "Africa-SE", "Africa-SthAm", "Europe-Asia",
"Europe-Europe", "Europe-SthAm", "MidEast-Asia", "MidEast-NthAm",
"MidEast-SE", "MidEast-SthAm", "SE Asia-Asia", "Sth Am.-Eur",
"Sth Am.-NthAm", "Sth Am.-SthAm"), class = "factor"), x = c(-30,
1, 20, 100, -30, 1, -100, -30, 20, -100, -30, 20, -30, 1, 20,
-100), y = c(120, 120, 120, 120, 1, 1, 1, -120, -120, -120, 100,
100, -100, -100, -100, -100), value = c(21, 22, 290, 100, 22,
3, 16, 1, 5, 6, 2, 62, 3, 3, 5, 3), col = structure(c(2L, 3L,
5L, 1L, 2L, 3L, 4L, 2L, 5L, 4L, 2L, 5L, 2L, 3L, 5L, 4L), .Label = c("brickred",
"green", "purple", "red", "yellow"), class = "factor"), region = structure(1:16, .Label = c("Asia-Africa",
"Asia-Eur", "Asia-ME", "Asia-SE", "Europe-Africa", "Europe-Eur",
"Europe-SthAm", "No. Am.-Africa", "No. Am.-ME", "No. Am.-Stham",
"SE Asia-Afr", "SE Asia-MI", "Sth. Am.-Africa", "Sth. Am.-Eur",
"Sth. Am.-ME", "Sth. Am.-SthAm"), class = "factor")), .Names = c("ID",
"x", "y", "value", "col", "region"), row.names = c(NA, -16L), class = "data.frame")

创建 map 很简单(尽管南极洲融化后看起来会更好!)。特鲁,目标不是等值线——根据某些变量的颜色区域——而是 Ari Lamstein 的包 choroplethr及其补充, choroplethrMaps ,轻松创建世界地图。
library(choroplethr)  
library(choroplethrMaps) # to obtain a map of the world
library(ggplot2)
library(riverplot)

data(country.map) # choose a world map
world <- ggplot(country.map, aes(long, lat, group=group)) +
geom_polygon(fill = "grey10") +
theme_bw()

outline map of countries

但是河图挫败了我,即使我得到了一个整体的情节, 是否有代码可以制作其中的四个(每个 LNG 导出地区一个)并将它们覆盖在世界地图上 ?
nodes.df <- df
nodes.df$ID <- capwords(as.character(nodes.df$ID))
nodes.df$ID <- as.factor(nodes.df$ID)

edges.df <- df
edges.df <- setNames(edges.df, c("ID", "N1", "N2", "Value", "Color", "Region"))

edges.df <- df[ , c("region", "x", "y", "value", "col", "region")] # use different ID names
edges.df <- setNames(edges.df, c("ID", "N1", "N2", "Value", "Color", "Region"))


river <- makeRiver(nodes = nodes.df, edges = edges.df, node_labels = NULL,
node_xpos = nodes.df$x, node_ypos = nodes.df$y)

Error in checkedges(edges, nodes$ID) :
edges must not have the same IDs as nodes

最佳答案

您可以单独创建桑基图,将它们保存为 PNG,重新导入它们并使用 grid.raster 将它们粘贴到 map 上。它与使用 Illustrator 一样耗费人力,但至少它是程序化的(例如,准备好进行数据更新)。当我们需要将桑基图与其他类似的图形结合起来时,我们之前在我的工作中做过这个。

关于r - 在世界地图上叠加多个河图(桑基图),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35120018/

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