R ggplot : "cross effect" in legend (not going away with show. 图例 = NA)

标签 r ggplot2 geom-hline geom-vline

以下代码会导致图例中出现不需要的交叉效应。

ggplot() + 
  geom_vline(aes(xintercept=1,colour="vertical"), show.legend = NA) +
  geom_hline(aes(yintercept=1,colour="horizontal"), show.legend = NA)

enter image description here

我读了几篇文章说添加 show.legend = NA可以使这种效果消失,但这在我的情况下不起作用。

编辑:
为了避免混淆,我不希望传奇消失!我只希望图例中的“十字架”消失,所以它应该显示如下项目:

enter image description here



enter image description here

最佳答案

我同意修复传说可能会变得棘手。对于这个例子,只需输入 show.legend = FALSE对于您不想要的 ONE 线。

library(ggplot2)

ggplot() + 
  geom_vline(aes(xintercept=1,colour="vertical"), show.legend = F) +
  geom_hline(aes(yintercept=1,colour="horizontal"))



好的,这是尝试 2。它很丑;它将两个传说塞进一个情节中。它看起来非常接近你想要的。
library(ggplot2)

p1 <- ggplot() + 
  geom_vline(aes(xintercept=1,colour="vertical"))+
  scale_color_manual(values = "#619CFF")

p2 <- ggplot()+
  geom_hline(aes(yintercept=1,colour="horizontal"))

l1 <- cowplot::get_legend(p1)
l2 <- cowplot::get_legend(p2)

p3 <- ggplot() + 
  geom_vline(aes(xintercept=1,colour="vertical")) +
  geom_hline(aes(yintercept=1,colour="horizontal"))+
  theme(legend.position = "none")

l3 <- cowplot::plot_grid(l1, l2, ncol = 1, align = "v")
cowplot::plot_grid(p3, l3, nrow = 1, align = "h", rel_widths = c(1, 0.2))

关于R ggplot : "cross effect" in legend (not going away with show. 图例 = NA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56562795/

相关文章:

r - 使用 do.call 在 R Shiny 中创建 ui

r - lavaan 纵向不变性 CFA 与 R 中的 2 因子模型

RMarkdown : how to get english error messages on a localised system

r - 增加 y 轴刻度标签 ggplot2 上的间距

r - 绘制 dr4pl 剂量 react 曲线,以及如何将它们与 ggplot2 集成?

r - 如何在 ggplot2 中使用 geom_ribbon 为 geom_hline 制作错误栏?

r - geom_hline 或 geom_vline 似乎不接受矢量作为引用线,如果在函数内部调用并使用 facet_grid()

r - 无法使用 FactoMineR 对双向列联表运行对应分析

r - 如何使用 geom_bar 在 ggplot2 中制作连接条形图?

r - 如何在图例中添加图例?