有办法在地图上绘制多个条形图吗?

时间:2019-12-19 16:04:47

标签: r ggplot2 mapping ggmap

我正在尝试在地图上绘制多个条形图,只是在寻找起点。我已经看了几个问题(如下所示)。

Barplots on a Map

Plotting bar charts on map using ggplot2?

How to plot barchart onto ggplot2 map

但是,所有这些似乎已经过时了。

以下是我要绘制的数据。我希望在一张地图上绘制4个地块,每个地理位置一个。我希望每个图都可以成为每个位置的每种目的的计数。

                 geoloc                     purpose count
1      Eastern Atlantic                    Behavior     4
2      Eastern Atlantic           Impacts/Fisheries     7
3      Eastern Atlantic                   Knowledge     8
4      Eastern Atlantic Migration/Habitat Selection     2
5      Eastern Atlantic                    Movement    10
7       Eastern Pacific                    Behavior     1
8       Eastern Pacific           Impacts/Fisheries     1
9       Eastern Pacific                   Knowledge     3
10      Eastern Pacific Migration/Habitat Selection     2
11      Eastern Pacific                    Movement     4
13 Southwestern Pacific                    Behavior     3
14 Southwestern Pacific                    Movement     7
15     Western Atlantic                    Behavior     8
16     Western Atlantic           Impacts/Fisheries     2
17     Western Atlantic                   Knowledge     8
18     Western Atlantic Migration/Habitat Selection     3
19     Western Atlantic                    Movement     9

这就是我获取要使用的地图的方式

mp <-  NULL
mapWorld <- borders("world", colour="gray70", fill="gray70") 
mp <- ggplot() +   mapWorld

我希望能够在ggplot2 / ggmap中做到这一点,因为这是我的习惯,但是很高兴学习其他解决方案!

这与我正在尝试做的事情类似(摘自Memarzadeh等人,2019年)。

enter image description here

1 个答案:

答案 0 :(得分:3)

我个人将使用magick软件包将图形视为图像,然后将图像与所需的偏移量合并以创建类似于您的目标的图像。我创建了一个非常简单的示例,向您展示了如何在世界地图only showing Atlantic and Pacific for now

上放置两个条形图

很明显,您可以执行进一步的操作来添加图例,图形标题等。这是我使用的代码

library(ggmap)
library(maps)
library(ggplot2)
library(magick)
mp <-  NULL
mapWorld <- borders("world", colour="gray70", fill="gray70") 

fig <- image_graph(width = 850, height = 550, res = 96)
ggplot() + mapWorld
dev.off()

df1 <- data.frame(name = c('A','B','C'), value = c(10,12,13))
df2 <- data.frame(name = c('A','B','C'), value = c(8,12,18))

bp1 <- ggplot(df1, aes(x = name, y = value, fill = name)) +
  geom_bar(stat = 'identity') +
  theme_bw() + 
  theme(legend.position = "none", axis.title.x = element_blank(), axis.title.y = element_blank())

bp2 <- ggplot(df2, aes(x = name, y = value, fill = name)) +
  geom_bar(stat = 'identity') +
  theme_bw() +
  theme(legend.position = "none", axis.title.x = element_blank(), axis.title.y = element_blank())

barfig1 <- image_graph(width = 100, height = 75, res = 72)
bp1
dev.off()

barfig2 <- image_graph(width = 100, height = 75, res = 72)
bp2
dev.off()

final <- image_composite(fig, barfig1, offset = "+75+150")
final <- image_composite(final, barfig2, offset = "+325+125")
final