我在 ggplot 中繪制圖例時遇到問題。我已經在網上搜索了幾個小時以尋找解決方案,但還沒有找到任何東西。
我正在嘗試創建一個具有不同線條和多邊形 shapefile 的 ggplot。我使用函式 geom_sf() 繪制 shapefile。繪圖不是問題。我的問題與傳奇創作有關。
目標是創建一個具有單個圖例并具有所有不同 shapefile 型別的圖,包括它們的顏色和/或填充 astetic。這意味著,線形應在圖例中表示為簡單的線,而多邊形應表示為簡單的多邊形。我已經設法做到了這一切。
主要困難在于其中一個多邊形(為了更好的可讀性)不僅使用“填充”引數作為 astetic,而且還使用“顏色”引數。
如果我創建一個圖,其中所有多邊形始終使用填充引數作為 astetic,所有線條形狀始終使用顏色引數作為 astetic,一切正常。但是,一旦我在圖中同時使用兩個引數的多邊形形狀,它就會破壞整個圖例結構。在這種情況下,例如,線 shapefile 的符號突然有一個框架和/或背景,盡管它們不應該有。
用 ggplot 構建這樣的東西是不可能的,還是我只是笨拙?
概括:
類似于下面創建的最小示例,我想在此圖中創建一個圖例,其中:
- A = 多邊形符號沒有邊框(如圖所示
) - B = 多邊形符號帶有邊框(如圖所示)并且
- C = 一條沒有邊框或背景的簡單線(如圖所示)。
我怎樣才能讓它作業?
library(ggplot2)
library(sf)
poly1 <- cbind(lon = c(5, 6, 7, 5), lat = c(52, 53, 51, 52))
poly2 <- cbind(lon = c(3, 5, 7, 3), lat = c(50, 52, 50, 50))
poly1 <- st_sf(st_sfc(st_polygon(list(poly1))))
poly2 <- st_sf(st_sfc(st_polygon(list(poly2))))
line <- st_sf(st_sfc(list(st_linestring(cbind(lon = c(5.5, 4.5), lat = c(53.5, 54.5))))))
ggplot()
geom_sf(data = poly1,
aes(fill = "A"),
colour = NA,
show.legend = "polygon")
geom_sf(data = poly2,
aes(fill = "B",
colour = "B"),
show.legend = "polygon")
geom_sf(data = line,
aes(colour = "C"),
show.legend = "line")
scale_fill_manual(name="Legend",
values = c("A" = "yellow", "B" = "green"),
guide = guide_legend(override.aes = list(linetype = c("blank","blank"), shape = NA))
)
scale_colour_manual(name="Legend",values = c("B" = "blue", "C" = "purple"),
guide = guide_legend(override.aes = list(linetype = c(0, 1))))
theme_bw()
uj5u.com熱心網友回復:
我也喜歡@AllanCameron 的回答,但這里有一種方法可以在沒有ggnewscale.
你一共有三把鑰匙。對于A和B,它們都應該表示為多邊形,但B應該有邊框,而A不應該。 C是奇數,因為它應該在圖例中用單獨的符號表示。
因此,這里的方法是讓A和B屬于同一個圖例(一個與color=和組合fill=),并將C保留為單獨的圖例。
對于A和B,我在兩者上都指定fill=and color=in ,然后使用函式aes()為兩者指定鍛煉顏色。scale_*_manual()
如果您分配color=給C within aes(),您將強制使用A和Bggplot2將其推入圖例中。這意味著我們需要在不同的審美下為C指定圖例!在這種情況下,我使用,但同樣適用于,等。alpha=linetypesize
p <-
ggplot()
geom_sf(
data = poly1, aes(fill = "A", colour = "A"))
geom_sf(
data = poly2, aes(fill = "B", colour = "B"))
geom_sf(
data = line, aes(alpha = "C"), colour = "purple")
scale_fill_manual(
name="Legend", values = c("A" = "yellow", "B" = "green"))
scale_colour_manual(
name="Legend", values = c("A" = "transparent", "B" = "blue", "C" = "purple"))
scale_alpha_manual(name=NULL, values=1)
guides(
fill=guide_legend(order=1, override.aes = list(linetype = c(0,1))),
colour= "none"
)
theme_bw()
p

要使圖例彼此更接近,您可以指定legend.spacingvia theme()。你必須玩弄這個數字,但它有效。在這種情況下,使legend.background透明也很重要,因為圖例相互夾在一起。
p theme(
legend.background = element_rect(fill=NA),
legend.spacing = unit(-15, "pt")
)

uj5u.com熱心網友回復:
您可以使用ggnewscale并設定每個幾何圖層的鍵字形來執行此操作:
ggplot()
geom_sf(data = poly1,
aes(fill = "A"),
colour = NA,
key_glyph = "rect")
scale_fill_manual(values = "yellow", name = NULL,
guide = guide_legend(order = 1))
ggnewscale::new_scale_fill()
ggnewscale::new_scale_color()
geom_sf(data = poly2,
aes(fill = "B",
colour = "B"),
key_glyph = "polygon")
scale_color_manual(values = "blue", name = NULL,
guide = guide_legend(order = 2))
scale_fill_manual(values = "green", name = NULL,
guide = guide_legend(order = 2))
ggnewscale::new_scale_color()
geom_sf(data = line,
aes(colour = "C"),
key_glyph = "path")
scale_colour_manual(values = "purple", name = NULL,
guide = guide_legend(order = 3))
theme_bw()

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