r - 排除 ggplot2 上图例条目中的箭头

标签 r ggplot2

我正在绘制一个geom_path(),我希望它在图中有箭头,但在图例中没有箭头。谷歌搜索表明,某种形式的东西

guides(color=guide_legend(override.aes = list(..)) 

将会得到答案,但我找不到 list() 所期望的文档。 guide_legend() 的帮助文件说“查看示例”以了解有关 override.aes 的更多信息,但只有一个示例展示了如何设置 alpha 级别传说。

为了使我想做的事情具体化,这里是生成带有箭头的图的代码,

data <- data.frame(x=c(1, 1, 1, 2),
                   y=c(1, 2, 1, 1),
                   color=c('color1', 'color1', 'color2', 'color2'))

library(ggplot2)
ggplot(data, aes(x=x, y=y, color=color, group=color)) +
  geom_path(size=2,
            arrow = arrow(angle = 30, 
                          length = unit(0.1, "inches"),
                          ends = "last", type = "closed")) +
  theme_bw()

这输出 Plot with arrowheads

但我正在寻找的是带有非箭头版本的图例,如

ggplot(data, aes(x=x, y=y, color=color, group=color)) +
  geom_path(size=2) +
  theme_bw()

enter image description here

谢谢!

最佳答案

一种稍微有点老套的方法是使用由 geom_line 创建的图例而不是箭头图例。在这里,我使用 show.legend = FALSE 调用 geom_path 来隐藏箭头图例。然后我使用geom_line,它只是在现有线条的顶部绘制线条(因此绘图不会改变),但为我们提供了一个没有箭头的图例。

library(tidyverse)
data = tibble(x=c(1, 1, 1, 2),
                  y=c(1, 2, 1, 1),
                  color=c('color1', 'color1', 'color2', 'color2'))

ggplot(data, aes(x=x, y=y, color=color, group=color)) +
  geom_path(
    size=2,
    arrow = arrow(
      angle = 30,
      length = unit(0.1, "inches"),
      ends = "last", type = "closed"
    ),
    show.legend = FALSE
  ) +
  geom_line(size = 2) +
  theme_bw()

关于r - 排除 ggplot2 上图例条目中的箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48591582/

相关文章:

r - 错误 `$<-.data.frame` (x, name, value) : replacement has 1 row, data has 0

r - 如何自动增加最高值和重要性栏之间的间距并在中间有重要性标签?

r - 将 gganimate 与 tweenr 一起使用时绘制标题

r - Sweave,包括源 R-Function 文件

r - 如何在 R shiny 中将下标或上标添加到 renderText

r - 随机选择ggtheme

r - 如何添加 geom_text 或 geom_label 以及相对于 geom_point 大小的位置?

r - 将顺序观察与 R 中的当前 ID 值和紧接在前的 ID 值链接在一起

r - 如何使用网格函数将y轴标题放在图形的左上角?

r - ggplot 颜色以具有预定义调色板的其他列为条件