gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 00:45:37 24 4
gpt4 key购买 nike

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com