关于r:使用美学和geom_text时从图例中删除\\’a \\’

Remove 'a' from legend when using aesthetics and geom_text

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

1
2
3
ggplot(data = iris, aes(x = Sepal.Length, y=Sepal.Width, shape = Species, colour = Species)) +
   geom_point() +
   geom_text(aes(label = Species))

geom_text中设置show.legend = FALSE

1
2
3
4
ggplot(data = iris,
       aes(x = Sepal.Length, y = Sepal.Width, colour = Species, shape = Species, label = Species)) +
    geom_point() +
    geom_text(show.legend = FALSE)

自变量show_guideggplot2 2.0.0中将名称更改为show.legend(请参见发行新闻)。

前-ggplot2 2.0.0

像这样的show_guide = FALSE ...

1
2
3
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 )

enter image description here


我有类似的问题。西蒙的解决方案对我有用,但需要稍加改动。我没有意识到我需要在geom_text的参数中添加" show_guide = F",而不是用它替换现有的参数-这是Simon的解决方案所显示的。对于像我这样的ggplot2菜鸟来说,这并不明显。一个适当的示例将使用OP的代码,并仅添加缺少的参数,如下所示:

1
2
3
..
geom_text(aes(label=Species), show_guide = F) +
..

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

下面是有关如何使用guide_legend()

的简短示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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

> </p>
<div class=

1
2
3
4
5
6
7
# Now let's override some of the aesthetics:
p + guides(
  fill = guide_legend(
    title ="Legend Title",
    override.aes = aes(label ="")
  )
)

> </p>
<p>由reprex软件包(v0.2.1)创建于2019-04-29 </p>
<div class=


就像尼克说的

以下代码仍会产生错误:

1
geom_text(aes(x=1,y=2,label="",show_guide=F))

enter image description here

而:

1
geom_text(aes(x=1,y=2,label=""),show_guide=F)

在aes参数之外消除了图例中的a

enter image description here


您还可以在geom_label_repel()的参数中使用show.legend = FALSE来删除图例中的" a"。
因此,而不是

1
2
3
4
5
6
7
8
9
10
11
12
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"
  )+ guides(
  fill = guide_legend(
    title ="Legend Title",
    override.aes = aes(label ="")
  )
)

你可以做,

1
2
3
4
5
6
7
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",
    show.legend = FALSE  )

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

希望对可能正在解决同一问题的任何人都有意义!