Boxplot in ggMarginal does not reflect what is on my plot

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

When I try running your code I get the following warnings from the last command:

Warning messages:
1: In (function (mapping = NULL, data = NULL, stat = "boxplot", position = "dodge2",  :
  Ignoring unknown parameters: `ggMarginalParams`
2: Continuous x aesthetic
ℹ did you forget `aes(group = ...)`? 

The first warning tells us that the parameter ggMarginalParams does not exist. Which version of ggExtra do you use? Mine is 0.10.0. The second warning is about missing group for the boxplot. In addition, documentation for ggMarginal states that if you provide the first argument (p = pworldmap), the second one (data) is ignored, so you don't need it.

But apart from that it seems (and I don't fully understand why) that coord_fixed messes up with ggMarginal. When you replace it with xlim(-10, 17) + ylim(35, 68) the plot looks fine.

Hi Marek,

Many thanks for this - so using your suggestion, the boxplot now reflects the map view correctly, but the map itself is now overstretched and some regions are clipped. Would you have an idea why this is happening?

Thanks!

coord_fixed keeps aspect ratio, xlim and ylim do not. You might have to tune one of the ranges (e.g. xlim) manually to get the correct aspect ratio. Alternatively, you can change the dimensions of the figure in the final publication, in Rmd/Quarto you can specify width and height of the figure.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.