R ggplot:图例中的“交叉效应”(show.legend = NA 不会消失)

Fra*_*ank 5 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)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

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

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

在此处输入图片说明

在此处输入图片说明

And*_*dS. 4

我同意修复传说可能会很棘手。对于此示例,只需输入show.legend = FALSE您不需要的一行即可。

library(ggplot2)

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

好的,这是尝试 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))
Run Code Online (Sandbox Code Playgroud)