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

时间:2013-08-20 14:29:03

标签: 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))

6 个答案:

答案 0 :(得分:112)

show.legend = FALSE中设置geom_text

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

参数show_guideshow.legendsee release news)中将名称更改为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 )

enter image description here

答案 1 :(得分:11)

我有similar problem。西蒙的解决方案对我有用,但需要稍微改动一下。我没有意识到我需要添加“show_guide = F”到geom_text的参数,而不是用现有的参数替换它 - 这正是Simon的解决方案所显示的。对于像我这样的ggplot2 noob,这并不是那么明显。一个恰当的例子会使用OP的代码,只是添加了这样的缺失参数:

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

答案 2 :(得分:8)

像尼克说的那样

以下代码仍会产生错误:

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

enter image description here

,而:

geom_text(aes(x=1,y=2,label=""),show_guide=F)
在aes参数之外的

消除了传说中的a

enter image description here

答案 3 :(得分:1)

您还可以在show.legend = FALSE的参数中使用geom_label_repel()来删除图例中的“ 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  )

答案 4 :(得分:0)

我们可以使用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 package(v0.2.1)于2019-04-29创建

答案 5 :(得分:0)

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

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