在guide_legend中使用label = FALSE删除图例标签,或在discrete_scale

时间:2015-07-19 12:29:49

标签: r ggplot2

作为一个带有几个传说的更复杂情节的一部分,其中一个传说只有一个条目。我想从此特定图例中删除标签,并仅保留(已编辑)标题。

df <- data.frame(x = 1, y = 1)

p <- ggplot(df, aes(x = x, y = y, color = factor(x))) + 
       geom_point()
p

enter image description here

因为我使用guides(foo = guide_legend(override.aes对图例进行了其他一些操作,所以我希望使用guide_legend参数删除同一label调用中的标签。

来自?guide.legend
label:[...]如果FALSE则标签不可见。”

因此,我试过了:

p + guides(color = guide_legend(title = "other",
                                label = FALSE))

但这会产生错误:

# Error in (function (name)  : grob 'NULL' not found

然后我看了discrete_scale,它有一个labels参数(现在带有“s”)。来自?discrete_scale
labelsNULL没有标签。”

因此,我试过了:

p + scale_color_discrete(name = "other",
                         labels = NULL)

...会产生错误:

# Error in data.frame(values = scale_map(scale, breaks), labels = I(scale_labels(scale)),  : 
#                      arguments imply differing number of rows: 1, 0

我目前的解决方法是:

p + scale_color_discrete(name = "other",
                         labels = "")

但是,如上所述,我更倾向于使用guide_legend调用删除标签。

所以我的主要问题是:
如何在label = FALSE工作中guide_legend

如果答案是“它只是不起作用”,那么“labelsNULL如何没有标签。”应该在scale_foo_bar中使用?

1 个答案:

答案 0 :(得分:1)

label = FALSE中的guide_legendlabels = NULL中的discrete_scale都已在开发版ggplot2_1.0.1.9002中修复。

# install development version, see https://github.com/hadley/ggplot2/.
# install.packages("devtools")
devtools::install_github("hadley/scales")
devtools::install_github("hadley/ggplot2movies")
devtools::install_github("hadley/ggplot2")

p + guides(color = guide_legend(title = "other", label = FALSE))
# or:
# p + scale_color_discrete(name = "other", labels = NULL)

enter image description here