是否可以在 ggplot 中創建與資料無關的自定義圖例(就像您使用 Photoshop 添加它一樣)?
library(ggplot2)
library(ggmap)
# ================ Mapping ===================
bas = get_stamenmap(c(-6.3, 53.25, -6, 53.40), zoom=7, maptype="terrain-background")
(mp <- ggmap(bas))
locs <- data.frame( lon_dec = c(-6.04015),
lat_dec = c( 53.3345))
(big_map <- mp
geom_point(data=locs, aes(x=lon_dec, y=lat_dec))
annotate(geom = "text", x = -6.04015, y = 53.3395, label = "1,2,3"))
代碼給出了下面的圖(除了我使用 Photoshop 手動添加了圖例):

我想要地圖旁邊的圖例說:
1- A
2- B
3- C
任何幫助,將不勝感激。
uj5u.com熱心網友回復:
要獲得這樣的自定義圖例,將其添加為自定義注釋可能是最簡單的。您將需要切換到CoordCartesian剪裁關閉來執行此操作。
(big_map <- mp
geom_point(data=locs, aes(x=lon_dec, y=lat_dec))
annotate(geom = "text", x = -6.04015, y = 53.3395, label = "1,2,3")
theme(plot.margin = margin(20, 100, 20, 20)))
big_map$coordinates <- coord_cartesian(clip = "off")
big_map annotation_custom(
grid::textGrob(x = 1.1, y = 0.5, label = "1 - A\n2 - B\n3 - C"))

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/488557.html
