繁体   English   中英

ggplot单值因子从图例中删除斜杠

[英]ggplot single-value factor remove slashes from legend

我想显示一个简单的geom_pointgeom_smoothgeom_abline以及有用的图例。 不幸的是, geom_pointgeom_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")

带有点,平滑,倾斜和残破图例的mtcar图

我查看了以下问题,但不了解如何适用于我的情况:

对我来说,解决此问题的最简单方法是根本不将所有内容都列为color 您可以使用sizeshapealpha等来分解图例。

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_pointgeom_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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM