如何从此代码生成的图例中删除字母'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))
答案 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_guide
在show.legend
(see 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 )
答案 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))
,而:
geom_text(aes(x=1,y=2,label=""),show_guide=F)
在aes参数之外的消除了传说中的a
答案 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
作为参数。
希望对可能正在解决同一问题的任何人都有意义!