我正在繪制一個柵格并用兩個多邊形覆寫它。我想分別為每個 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()

uj5u.com熱心網友回復:
在 ggplot2 中geom沒有圖例。相反,傳說反映了美學或尺度。geom 只決定圖例中使用的鍵的形狀,例如 ageom_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"))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/381238.html
標籤:r ggplot2 geom-raster
