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

标签 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

最佳答案

这是color美学的图例,它尝试结合geom_pointgeom_linegeom_hline<的所有所需信息geom_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

关于r - 如何删除添加到图例中的斜线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54774780/

相关文章:

r - 在sp中添加CRS似乎不一致

r - 为什么 R 中的 h2o.randomForest 比 randomForest 包做出更好的预测

r - 将列均值和行均值添加到 r 中的矩阵

r - ggplot2:无法更改位置 = 'right' 的axis.title.y 角度

r - 如何添加不同大小和颜色的 ggplot2 字幕?

r - 使用 ggplot2 堆叠条形图标签

r - 如何在 R 中转换 Redis 日期

r - 如何在段的开头添加标签到geom_segment?

r - ggplot2-scale_colour_manual和scale_shape手动问题

r - 将应用结果分配给数据框的多列