我寫了這段代碼:
ggplot()
geom_sf(aes(fill = dat$color_province))
theme_void()
geom_point(data = producer,
aes(x = producer$MX, y = producer$MY), size = 3, col = "green", shape = 17, alpha = 0.6)
geom_point(data = distribution,
aes(x = distribution$MX, y = distribution$MY), size = 4.5, col = "yellow", shape = 15)
geom_point(data = retailer,
aes(x = retailer$MX, y = retailer$MY), size = 3, col = "slateblue", shape = 16)
geom_point(data = Demand,
aes(x = Demand$MX, y = Demand$MY, size = Demand$De), col = "slateblue", shape = 17, alpha = 0.7)
scale_fill_manual(values = c("#ff3333", "#ffc266"),
name = "Situation")
現在我想添加一個圖例來識別我的情節中的所有點。我該怎么做?
uj5u.com熱心網友回復:
這是每個人都可以運行的一些資料的示例,因為它使用了 R 附帶的內置資料集。在這里,我將顏色和大小以系列名稱作為動態美學,然后將這些系列值映射到不同的美學值使用scale_*_manual,*你想在哪里改變系列的美學。這會生成一個自動圖例。通過為每個美學賦予相同的名稱(此處為“源”),ggplot2 知道將它們組合成一個圖例。
(順便說一句,使用 ggplot2 美學中的表格參考變數是不必要的并且可能導致錯誤retailer$MY;每個幾何圖形都會假設變數在參考的資料框中data =,因此您可以MY在這種情況下使用。)
ggplot()
geom_point(data = mtcars,
aes(x = wt, y = mpg, color = "mtcars", size = "mtcars"))
geom_point(data = attitude,
aes(x = rating/20, y = complaints/3, color = "attitude", size = "attitude"))
scale_color_manual(values = c("mtcars" = "slateblue", "attitude" = "red"), name = "source")
scale_size_manual(values = c("mtcars" = 3, "attitude" = 4.5), name = "source")

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/406995.html
標籤:
