0

我正在绘制一个栅格并用两个多边形覆盖它。我想分别为每个 shapefile 绘制图例(即缓冲区图例和每个多边形的一个图例)。我show.legend = TRUE在里面使用过,geom_path但它没有给我想要的东西。我正在使用此代码。

gplot(pfiles) + 
  geom_raster(aes(fill = factor(value))) +
  geom_path(data=ward, aes(long, lat, group=group), color = "black") +
  geom_path(data=buffer, aes(long, lat, group=group), color = "red") +
  facet_wrap(~ variable, ncol = 4) +
  scale_fill_manual(values = c("darkorchid", "chartreuse3", "cornflowerblue", "goldenrod2"), 
                      na.value="transparent", na.translate = F) +
  guides(fill = guide_legend(title = "Buffer Zones")) +
  #scale_color_identity(guide = "legend") +
  theme_bw() +
  theme(axis.text = element_blank(), legend.position = "right", legend.direction = "vertical",
        axis.title = element_blank(),
        legend.key.height = unit(2, "cm"), legend.key.width = unit(1, "cm"),
        legend.title = element_text(size = 22, face = "bold"), legend.text = element_text(size = 20), 
        strip.text = element_text(size = 18, face = "bold"),
        plot.title = element_text(size = 20, face = "bold"),
        plot.subtitle = element_text(size = 15), plot.caption = element_text(size = 12, face = "bold.italic"),
        panel.background = element_rect(fill = "transparent")) +
  labs(title = "Variables for Identification of Potential Urban Development Zones",
       subtitle = "Land Price and Ground Water Level represent price and water depth respectively; Others represent Euclidean Distance") +
  coord_equal()

在此处输入图像描述

4

1 回答 1

1

在 ggplot2geom中没有图例。相反,传说反映了美学或尺度。geom 仅确定图例中使用的键的形状,例如,ageom_point将被描绘为一个点, a 将被描绘为geom_path一条线,......这就是说,如果你想要一个图例,你必须在美学上进行映射。

要获得描绘不同颜色geom_paths 的图例,您可以在美学上映射一个常数值color,然后使用 将您想要的颜色分配给这些常数scale_color_manual

以下代码未经测试,但应为您提供所需的结果:

ggplot(pfiles) +
  geom_raster(aes(fill = factor(value))) +
  geom_path(data = ward, aes(long, lat, group = group, color = "ward")) +
  geom_path(data = buffer, aes(long, lat, group = group, color = "buffer")) +
  scale_color_manual(values = c(ward = "black", buffer = "red"))
于 2021-12-10T21:48:56.537 回答