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

194

我该如何从这段代码生成的图例中删除字母'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))
7个回答

241

geom_text中设置show.legend = FALSE

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

show_guide参数在ggplot2 2.0.0中更名为show.legend请参见发行新闻)。


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  = FALSE)

输入图像描述


6
ggplot2 3.2.1 中将 show.legend 设为 FALSE 将完全删除图例! - NelsonGon
1
(a) 在 ggplot2 3.3.5 中,show.legend=FALSE 可以保留整体图例。 (b) 我很惊讶发现这个问题有多么困难。 - learner
当将aes()部分包含在geom.txt中时,似乎无法正常工作,并返回"忽略未知的美学属性:show.legend"。 - Markm0705

31
我们可以使用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

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

这段文字是由reprex包(v0.2.1)于2019-04-29创建的。


2
我认为这比被接受的解决方案更好,因为它允许从图例中特定地删除字母“a”,而其他美学元素可以保持不变(如果需要的话)。 - Markel
如果使用颜色而不是填充,可以使用guides( color = guide_legend(override.aes = aes(label = ""))) - Pipe Marulanda

17

我有一个类似的问题。Simon's的解决方案对我有用,但需要稍微改动一下。我没有意识到我需要在geom_text的参数中添加“show_guide = F”,而不是替换掉现有的参数——这正是Simon's的解决方案所示。对于像我这样的ggplot2新手来说,这不是那么明显的。一个适当的例子应该使用原作者的代码,然后只需添加缺少的参数就像这样:

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

1
这对我有用,但是 show_guide 现在已经被弃用了。请使用 show.legend 代替。 - Alison Bennett

8

像Nick所说的那样,以下代码仍会产生错误:

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

这里输入图片描述

相比之下:

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

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

输入图片描述


3
有没有办法将字母 'a' 改为其他的字母,比如说 'r'? - asalimih

5
我曾经遇到一个类似的问题,当我试图用geom_text_repel标记不同颜色的点时,会在点后面出现字母'a'。为了让点只显示,不显示'a',我需要在geom_text_repel中添加参数show.legend=FALSE。希望这对正在遇到同样问题的人有所帮助!

3
您可以在geom_label_repel()的参数中使用show.legend = FALSE来删除图例中的“a”。因此,而不是。
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 = "")
  )
)

你可以做到。
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  )

2

基于最佳答案进行扩展。如果您只想让geom_text()可见,但需要geom_point()用于图例,您可以将alpha设置为0,使其不可见,但在指南中覆盖为1以强制显示。

ggplot(data = iris, aes(x = Sepal.Length, y=Sepal.Width, 
                        shape = Species, colour = Species)) + 
  geom_point(alpha = 0) + 
  geom_text(aes(label = Species)) +
  guides(color = guide_legend(override.aes = aes(label = "", alpha = 1))) 

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接