还有另一个贴标机问题...我正在ggplot2
> 3的情况下在贴标机中使用数学表达式
library(ggplot2)
var.lab1 = as_labeller(c(
"setosa" = "se",
"versicolor" = "ve",
"virginica" = "vi"
))
var.lab2 = as_labeller(c(
"setosa" = bquote("Spp"[set]),
"versicolor" = bquote("Spp"[ver]),
"virginica" = bquote("Spp"[vir])
))
这按预期工作
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) +
facet_wrap(~ Species, labeller = var.lab1) +
geom_point()
这不起作用(贴标机无效)
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) +
facet_wrap(~ Species, labeller = var.lab2) +
geom_point()
这可行
var.lab3 = c(
"setosa" = bquote("Spp"[set]),
"versicolor" = bquote("Spp"[ver]),
"virginica" = bquote("Spp"[vir])
)
vlabeller <- function (variable, value) {
return(var.lab3[value])
}
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) +
facet_wrap(~ Species, labeller = vlabeller) +
geom_point()
但是ggplot2
不满意“警告消息:Labeller API已更新。现在不建议使用带有变量和value参数的Labeller。请参阅labellers文档。”
答案 0 :(得分:2)
您可以只使用label_bquote
和substr
来获得想要的东西。无需自定义贴标机。
library(ggplot2)
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) +
facet_wrap(~ Species, labeller = label_bquote(cols = "Spp"[.(substr(Species, 1, 3))])) +
geom_point()
由reprex package(v0.2.1)于2018-11-22创建