作为一个带有几个传说的更复杂情节的一部分,其中一个传说只有一个条目。我想从此特定图例中删除标签,并仅保留(已编辑)标题。
df <- data.frame(x = 1, y = 1)
p <- ggplot(df, aes(x = x, y = y, color = factor(x))) +
geom_point()
p
因为我使用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
:
“labels
:NULL
没有标签。”
因此,我试过了:
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
?
如果答案是“它只是不起作用”,那么“labels
:NULL
如何没有标签。”应该在scale_foo_bar
中使用?
答案 0 :(得分:1)
label = FALSE
中的guide_legend
和labels = 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)