有办法在地图上绘制多个条形图吗? [英] Any way to plot multiple barplots on a map?
问题描述
我正在尝试在地图上绘制多个条形图,只是在寻找起点.我已经看了几个问题(如下所示).
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)..
但是,所有这些似乎都已经过时了.
However, all of these seem out of date.
以下是我要绘制的数据.我希望在一张地图上绘制4个地块,每个地理位置一个.我希望每个图都可以成为每个位置的每种目的的计数.
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
我希望能够在ggplot2/ggmap中做到这一点,因为这是我的习惯,但是很高兴学习其他解决方案!
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!
这与我正在尝试做的事情类似(来自Memarzadeh等人,2019年).
This is similar to what I am trying to do (from Memarzadeh et al. 2019).
推荐答案
我个人将使用magick软件包将图形视为图像,然后将图像与所需的偏移量合并以创建类似于您的目标的图像.我创建了一个非常简单的示例,向您展示了如何在世界地图上放置两个条形图
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
这篇关于有办法在地图上绘制多个条形图吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!