Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to plot multiple barplots on a map?

Tags:

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).

enter image description here

like image 554
ljh2001 Avatar asked Dec 19 '19 16:12

ljh2001


People also ask

How do you plot two bar graphs in Matlab?

bar( y ) creates a bar graph with one bar for each element in y . To plot a single series of bars, specify y as a vector of length m. The bars are positioned from 1 to m along the x-axis. To plot multiple series of bars, specify y as a matrix with one column for each series.

How do I plot two bar charts side by side in Python?

By using plt. subplot() method we create two subplots side by side. plt. bar() method is used to create multiple bar chart graphs.


1 Answers

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 only showing Atlantic and Pacific for now

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
like image 90
DanStu Avatar answered Oct 18 '22 10:10

DanStu