Any way to plot multiple barplots on a map?

只谈情不闲聊 提交于 2020-04-13 06:33:29

问题


I'm attempting to plot multiple barplots on a map and am just looking for a place to start. I've looked at a few questions already (shown below)..

Barplots on a Map

Plotting bar charts on map using ggplot2?

How to plot barchart onto ggplot2 map

However, all of these seem out of date.

Below is the data I am trying to plot. I'm looking to make 4 plots on one map with one in each geolocation. I want each plot to be a barpot of counts for each purpose at each respective location.

                 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

This is how I obtained the map I am trying to use

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

I would like to be able to do this in ggplot2/ggmap since that is what I am used to, but would be happy to learn other solutions!

This is similar to what I am trying to do (from Memarzadeh et al. 2019).


回答1:


I would personally use the magick package to treat the graphs as images, and merge the images with the desired offsets to create something that resembles your goal. I created a very quick example which shows you how this might work to place two bar graphs on the world map

Obviously, you could perform further manipulation to add a legend, graph titles etc. Here is the code I used

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


来源:https://stackoverflow.com/questions/59413256/any-way-to-plot-multiple-barplots-on-a-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!