我想显示一个简单的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")
我看过以下问题,但不了解如何适用于我的情况:
答案 0 :(得分:0)
对我来说,解决此问题的最简单方法是根本不将所有内容都列为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")
答案 1 :(得分:0)
我的猜测:color
和geom_point
都使用geom_smooth
,因此图例尝试将两个几何合并。
当您使用其他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 = "")
希望有帮助!