如何删除添加到图例的斜线?

时间:2019-02-19 20:56:04

标签: r ggplot2

如何删除添加到图例的斜线?还有黄色和灰色的点...为什么会发生?

library(ggplot2)
x <- seq(from = 1, to = 10, by = 1)
df = data.frame(x=x, y=x^2, v=2*x)
df2 = data.frame(x=x, y=x^2-5*x-10)
ggplot(df, aes(x, y)) + 
  geom_point(aes(size = v)) + 
  theme_classic() +   
  scale_size("blabla") +     
  geom_point(data=df2, aes(x, y, color = "blue")) +
  geom_line(data=df2, aes(x, y, color = "blue")) +
  geom_hline(aes(color="gray",yintercept=25)) +
  geom_abline(aes(color="yellow", intercept=0, slope=1)) +
  scale_color_manual(values = c("blue","gray","yellow"), labels = c("nanana","hhh","abab"), name = "other")

enter image description here

1 个答案:

答案 0 :(得分:3)

这就是color美学的传奇,它试图结合geom_pointgeom_linegeom_hlinegeom_abline的所有必要信息。要摆脱这些限制,我们需要

geom_abline(aes(color = "yellow", intercept = 0, slope = 1), show.legend = FALSE)

我们必须添加点

guides(color = guide_legend(override.aes = list(shape = c(19, NA, NA))))

这给

enter image description here