在世界地图上叠加多个河图(桑基图)
Overlay multiple riverplots (Sankey diagrams) on a map of the world
下面这张引人入胜的图表来自 经济学人,2016 年 1 月 30 日,第 61 页。它描绘了从五个地区到六个地区的液化天然气 (LNG) 出口情况。 R 如何绘制类似的东西,也许有几个 Sankey 图(来自包 riverplots
)但箭头与描绘的箭头相反?意思是,流入出口区域的流量将显示为流出。
通过眼球从绘图中提取数据导致 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")
创建地图很简单(尽管南极洲融化后看起来会更好!)。诚然,我们的目标不是等值线——根据某个变量的彩色区域——而是 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()
但是河图挫败了我,即使我得到了一个整体图,是否有代码可以制作四个(每个 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
您可以单独创建 Sankey 图表,将它们另存为 PNG,重新导入它们并使用 grid.raster 将它们粘贴到地图上。它与使用 Illustrator 一样劳动密集,但至少它是程序化的(例如准备好数据更新)。我们以前在工作中需要将 Sankey 图表与其他类似图形组合时就这样做过。
您可以使用 https://www.mapprovision.com/ 来执行此操作。
它获取如下所示的数据,然后完成其余的工作。
这是一个例子:
这里有分步指南:
https://mapprovision.blogspot.com/2020/01/how-to-create-your-own-world-sankey-maps.html
- 免责声明 - 我在 MapProvision 工作
下面这张引人入胜的图表来自 经济学人,2016 年 1 月 30 日,第 61 页。它描绘了从五个地区到六个地区的液化天然气 (LNG) 出口情况。 R 如何绘制类似的东西,也许有几个 Sankey 图(来自包 riverplots
)但箭头与描绘的箭头相反?意思是,流入出口区域的流量将显示为流出。
通过眼球从绘图中提取数据导致 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")
创建地图很简单(尽管南极洲融化后看起来会更好!)。诚然,我们的目标不是等值线——根据某个变量的彩色区域——而是 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()
但是河图挫败了我,即使我得到了一个整体图,是否有代码可以制作四个(每个 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
您可以单独创建 Sankey 图表,将它们另存为 PNG,重新导入它们并使用 grid.raster 将它们粘贴到地图上。它与使用 Illustrator 一样劳动密集,但至少它是程序化的(例如准备好数据更新)。我们以前在工作中需要将 Sankey 图表与其他类似图形组合时就这样做过。
您可以使用 https://www.mapprovision.com/ 来执行此操作。
它获取如下所示的数据,然后完成其余的工作。
这是一个例子:
这里有分步指南:
https://mapprovision.blogspot.com/2020/01/how-to-create-your-own-world-sankey-maps.html
- 免责声明 - 我在 MapProvision 工作