[英]ggplot single-value factor remove slashes from legend
我想顯示一個簡單的geom_point
, geom_smooth
和geom_abline
以及有用的圖例。 不幸的是, geom_point
和geom_smooth
的簡單組合在點圖例上放置了一條水平線,而在所有圖例上添加geom_abline
放置了對角斜線。
如何在圖例框僅包含“點”,“線”和“虛線”的地方創建簡單的視覺效果?
謝謝
例子:
Geom_point和geom_smooth
mtcars %>%
ggplot() +
geom_point(aes(x = carb, y = mpg, color = "Points")) +
geom_smooth(aes(x = carb, y = mpg, color = "Trendline")) +
theme(legend.position="bottom") +
labs(x = "carb",
y = "mpg",
color = "LEGEND")
Geom_point,geom_smooth和geom_abline
mtcars %>%
ggplot() +
geom_point(aes(x = carb, y = mpg, color = "Points")) +
geom_smooth(aes(x = carb, y = mpg, color = "Trendline")) +
geom_abline(aes(slope = 1, intercept = 10, color = "ZCustom"), linetype = "dashed") +
theme(legend.position="bottom") +
labs(x = "carb",
y = "mpg",
color = "LEGEND")
修復了geom_point圖例,但在其他圖例上保留了斜線
mtcars %>%
ggplot() +
geom_point(aes(x = carb, y = mpg, color = "Points")) +
geom_smooth(aes(x = carb, y = mpg, color = "Trendline")) +
geom_abline(aes(slope = 1, intercept = 10, color = "ZCustom"), linetype = "dashed") +
scale_color_manual(values = c("red", "blue", "black"),
label = c("Points", "Trendline", "Custom"),
guide = guide_legend(override.aes = list(
linetype = c("blank", "solid", "dashed"),
shape = c(16, NA, NA)))) +
theme(legend.position="bottom") +
labs(x = "carb",
y = "mpg",
color = "LEGEND")
我查看了以下問題,但不了解如何適用於我的情況:
對我來說,解決此問題的最簡單方法是根本不將所有內容都列為color
。 您可以使用size
, shape
, alpha
等來分解圖例。
mtcars %>%
ggplot() +
geom_point(aes(x = carb, y = mpg, shape = "")) +
geom_smooth(aes(x = carb, y = mpg, alpha = "")) +
geom_abline(aes(slope = 1, intercept = 10, color = ""), linetype = "dashed") +
theme(legend.position="bottom") +
labs(x = "carb",
y = "mpg",
shape = "Points",
alpha = "Trendline",
color = "ZCustom")
我的猜測: geom_point
和geom_smooth
都使用了color
,因此圖例嘗試將兩種geoms結合使用。 當您使用其他aes
,圖例會將它們作為單獨的屬性/層。
mtcars %>%
ggplot( aes(x = carb, y = mpg) ) +
geom_point( aes(fill = "Points") ) + # use 'fill' rather than 'color'
geom_smooth( aes(color = "Trendline") ) +
theme(legend.position = "bottom") +
labs(x = "carb", y = "mpg", color = "", fill = "")
希望能幫助到你!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.