使用美学和geom_text时,从图例中删除"a"

use*_*207 107 r ggplot2 aesthetics

如何从此代码生成的图例中删除字母"a"?如果我删除了geom_text,那么'a'字母将不会显示在图例中.不过我想保留geom_text.

ggplot(data = iris, aes(x = Sepal.Length, y=Sepal.Width, shape = Species, colour = Species)) + 
   geom_point() + 
   geom_text(aes(label = Species))
Run Code Online (Sandbox Code Playgroud)

Sim*_*lon 121

设置show.legend = FALSEgeom_text:

ggplot(data = iris,
       aes(x = Sepal.Length, y = Sepal.Width, colour = Species, shape = Species, label = Species)) + 
    geom_point() +
    geom_text(show.legend = FALSE)
Run Code Online (Sandbox Code Playgroud)

该参数show_guide将名称更改为show.legendin ggplot2 2.0.0(请参阅发布新闻).


ggplot2 2.0.0:

有了show_guide = FALSE这样的...

ggplot( data=iris, aes(x=Sepal.Length, y=Sepal.Width , colour = Species , shape = Species, label = Species ) , size=20 ) + 
geom_point()+
geom_text( show_guide  = F )
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 在 `ggplot2` 3.2.1 中将 `show.legend` 设置为 `FALSE` 将完全删除图例! (5认同)

Kam*_*ski 14

我们可以使用guide_legend(override.aes = aes(...))隐藏图例中的“a”。

下面是一个关于如何使用guide_legend()的简短示例

library(ggrepel)
#> Loading required package: ggplot2

d <- mtcars[c(1:8),]

p <- ggplot(d, aes(wt, mpg)) +
  geom_point() +
  theme_classic(base_size = 18) +
  geom_label_repel(
    aes(label = rownames(d), fill = factor(cyl)),
    size = 5, color = "white"
  )

# Let's see what the default legend looks like.
p
Run Code Online (Sandbox Code Playgroud)

# Now let's override some of the aesthetics:
p + guides(
  fill = guide_legend(
    title = "Legend Title",
    override.aes = aes(label = "")
  )
)
Run Code Online (Sandbox Code Playgroud)

reprex 包(v0.2.1)于 2019 年 4 月 29 日创建

  • 我认为这是比公认的解决方案更好的解决方案,因为它允许专门从图例中删除“a”字母,而如果需要,其他美学可以保持不变。 (2认同)

Nic*_*ick 11

我遇到了类似的问题.西蒙的解决方案对我有用,但需要稍微改动一下.我没有意识到我需要在geom_text的参数中添加 "show_guide = F",而不是用现有的参数替换它 - 这就是Simon的解决方案所显示的.对于像我这样的ggplot2 noob,这并不是那么明显.一个恰当的例子会使用OP的代码,只是添加了缺少的参数,如下所示:

..
geom_text(aes(label=Species), show_guide = F) +
..
Run Code Online (Sandbox Code Playgroud)


use*_*238 8

像尼克说的那样

以下代码仍会产生错误:

geom_text(aes(x=1,y=2,label="",show_guide=F))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

然而:

geom_text(aes(x=1,y=2,label=""),show_guide=F)
Run Code Online (Sandbox Code Playgroud)

在aes论证之外消除了传说中的a

在此输入图像描述

  • 有没有办法将“a”自定义为“r”等其他内容? (3认同)

arr*_*vis 5

我遇到了类似的问题,在我试图用 标记的不同颜色的点后面出现了一个“a” geom_text_repel。要删除“a”,以便它只显示后面没有“a”的点,我必须show.legend=FALSE在 中添加作为参数geom_text_repel

希望这对任何可能遇到同样问题的人来说都是有意义的!