为 geom_raster 和 geom_path 添加单独的图例
Add separate legend for geom_raster and geom_path
我正在绘制一个栅格并用两个多边形覆盖它。我想分别为每个 shapefile 绘制图例(即缓冲区图例和每个多边形的一个图例)。我在 geom_path
中使用了 show.legend = TRUE
但它没有给我我想要的东西。我正在使用此代码。
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()
在 ggplot2 中 geom
s 没有图例。相反,传说反映了美学或规模。 geom 仅确定图例中使用的键的形状,例如a geom_point
会被描绘成一个点,a geom_path
会被描绘成一条线,... 这就是说,如果你想有一个传奇,你必须在美学上进行映射。
要获得描述不同颜色 geom_path
的图例,您可以在 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"))
我正在绘制一个栅格并用两个多边形覆盖它。我想分别为每个 shapefile 绘制图例(即缓冲区图例和每个多边形的一个图例)。我在 geom_path
中使用了 show.legend = TRUE
但它没有给我我想要的东西。我正在使用此代码。
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()
在 ggplot2 中 geom
s 没有图例。相反,传说反映了美学或规模。 geom 仅确定图例中使用的键的形状,例如a geom_point
会被描绘成一个点,a geom_path
会被描绘成一条线,... 这就是说,如果你想有一个传奇,你必须在美学上进行映射。
要获得描述不同颜色 geom_path
的图例,您可以在 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"))