I have mapped species distribution data onto a world map by creating a scatterplot, then adding a world map ontop of it.
I have then added a boxplot to the right side of the map.
However, the boxplot does not reflect what is shown on the map itself - the boxplot is correctly reflecting the species distributions BUT on a full world-map scale. I would like the boxplot to reflect exactly what is shown on the map itself, so the dots match.
Below is a short snippet of the data in question.
And ere is my current annotated code which I'm stuck on:
library(ggplot2)
library(ggExtra)
# create world map
world_map <- map_data("world")
library(ggExtra)
Vulgata_coastal_only_decdeg <- read.csv("Vulgata_coastal_decdeg.csv")
Vulgata_coastal_only_decdeg
# create scatter plot of species coordinates
pscatterplot <- ggplot(Vulgata_coastal_only_decdeg, aes(x = Longitude, y = Latitude)) +
geom_point(color = "black", size = 3.5) +
coord_fixed(xlim = c(-10, 17), ylim = c(35, 68))
# add world map as background
pworldmap <- pscatterplot +
geom_polygon(data = world_map, aes(x = long, y = lat, group = group),
fill = "white", color = "grey") +
ggtitle("Patella vulgata")
pworldmap
# add boxplot to the right using ggmarginal
# for this first create a new data frame which has only the coordinates within the map restriction
library(dplyr)
# filter Vulgata_coastal_only_decdeg based on map restrictions
filtered_data <- Vulgata_coastal_only_decdeg %>%
filter(Longitude >= -10 & Longitude <= 17 & Latitude >= 35 & Latitude <= 68)
filtered_data
pboxplot <- ggMarginal(pworldmap, data = filtered_data,
margins = "y", type = "boxplot", size = 5, color = "black",
ggMarginalParams = list(params = list(group = filtered_data$Species)))
pboxplot
Vulgata_coastal_only_decdeg
Species Latitude Longitude
1 Patella vulgata 51.69 -4.69
2 Patella vulgata 49.96 -6.35
3 Patella vulgata 51.33 1.41
4 Patella vulgata 55.04 -1.43
5 Patella vulgata 58.42 -5.10
6 Patella vulgata 60.78 -0.82
7 Patella vulgata 59.02 -3.36
8 Patella vulgata 50.50 -3.51
9 Patella vulgata 52.25 -4.26
10 Patella vulgata 54.15 -0.19
11 Patella vulgata 50.65 -1.47
12 Patella vulgata 54.56 -3.59
13 Patella vulgata 60.17 -1.31
14 Patella vulgata 54.84 -1.33
15 Patella vulgata 59.95 -1.34