0

I am using patchwork to plot 3 maps while two have a common legend map

Here is the code to produce the maps and plot them:

#MAP1: SIF/SM
map1=ggplot(data = sif_min_sm2) +
  geom_tile(aes(x = lon, y = lat, fill = cut(SIF_SM, breaks = my_breaks, include.lowest = TRUE))) +
  coord_equal() +
  labs(x = "Longitude", y = "Latitude") +
  scale_fill_manual(
    values = my_colors,
    labels = levels(break_labels),
    na.value = "white",
    name = "SIF anomalies",
    limits = levels(break_labels)
  ) +
  ggtitle("SIF anomalies for driest SM month") +
  theme_minimal() +
  theme(
    panel.background = element_blank(),
    plot.title = element_text(size = 14, face = "bold"),
    legend.position = "right",
    legend.key.size = unit(1, "lines"), 
    legend.text = element_text(size = 8), 
    legend.key.height = unit(1, "cm"),
    legend.margin = margin(t = 0, r = 5, b = 0, l = 0)
  )



#Map 2: SIF/VPD
map2=ggplot(data = sif_min_vpd2) +
  geom_tile(aes(x = lon, y = lat, fill = cut(SIF_VPD, breaks = my_breaks, include.lowest = TRUE))) +
  coord_equal() +
  labs(x = "Longitude", y = "Latitude") +
  scale_fill_manual(
    values = my_colors,
    labels = levels(break_labels),
    na.value = "white",
    name = "SIF anomalies",
    limits = levels(break_labels)
  ) +
  ggtitle("SIF anomalies for driest VPD month") +
  theme_minimal() +
  theme(
    panel.background = element_blank(),
    plot.title = element_text(size = 14, face = "bold")
  )


#Map 3: VPD vs SM for lowest SIF anomalie

map3=ggplot(data = test) +
  geom_tile(aes(x = lon, y = lat, fill = min_column)) +
  coord_equal() +
  labs(x = "Longitude", y = "Latitude") +
  scale_fill_brewer(palette="Set1", direction=1 )+
  ggtitle("SIF anomalies for driest SM/VPD month") +
  theme_minimal()+
  theme(panel.background = element_blank(),
        plot.title = element_text(size = 14, face = "bold"),
        legend.title = element_blank())

#common legend


combined <- (map1 / map2) &
  theme(legend.position = "right",
        legend.key.size = unit(1, "lines"),  # Increase the size of the legend keys
        legend.text = element_text(size = 8), 
        legend.key.height = unit(0.9, "cm"),# Reduce the size of the legend text
        legend.margin = margin(t = 0, r = 5, b = 2, l = 0), 
        legend.box.spacing = unit(2, "cm")) # Add some margin to the legend
(combined /map3) + plot_layout(guides = "collect") 

I would like the first legend 'SIF anomalies' to be more aligned to the first two maps as well as for the second legend to the third map.

I have tried many things following some post' suggestions on the forum: removing the legend settings inside each map; using the guide_area function or to modify the settings of the patchwork function but whenever I try it does not work or it says that 'Manual legend position not possible for collected guides. Defaulting to 'right''.

1 Answers1

0

somehow by combining ggarange and patchwork, I could get the desired results:

# combine the first two maps with common legend
combined <- ggarrange(map1, map2, ncol = 1, common.legend = TRUE, legend = "right") +
  theme(legend.position = "right",
        legend.key.size = unit(1, "lines"),
        legend.text = element_text(size = 8), 
        legend.key.height = unit(0.9, "cm"),
        legend.box.spacing = unit(3, "cm"))

#add the third map and adjust its legend position
(combined + plot_layout(guides = "collect")) / 
  (map3 + theme(legend.position = c(1.1, 0),
                legend.justification = c(1, 0),
                legend.margin = margin(0, 0, -10, 0),
                legend.box.spacing = unit(0.1, "cm")))