R语言 拼板+图例位置调整失败

628mspwn  于 2023-04-03  发布在  其他
关注(0)|答案(1)|浏览(118)

我使用拼凑绘制3Map,而两个有一个共同的传说x1c 0d1x
下面是生成Map并绘制它们的代码:

#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")

我想第一个传说'应力强度因子异常',以更符合前两张Map,以及为第二个传说,以第三张Map。
我已经尝试了很多东西以下一些帖子'在论坛上的建议:移除每个Map内的图例设置;使用guide_area函数或修改patchwork函数的设置,但每当我尝试时,它都不起作用,或者它说“手动图例位置不可能用于收集的指南。默认为“右””。

fhity93d

fhity93d1#

通过结合ggarange和patchwork,我可以得到想要的结果:

# 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")))

相关问题