gpt4 book ai didi

r - 带有 bquote 的 ggplot2 facet labeller

转载 作者:行者123 更新时间:2023-12-04 15:52:09 25 4
gpt4 key购买 nike

另一个贴标机问题...我正在努力使用 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不高兴“警告消息:标签器 API 已更新。现在不推荐使用变量和值参数的标签器。请参阅标签器文档。”

最佳答案

你可以使用 label_bquotesubstr得到你想要的。无需定制贴标机。

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()



创建于 2018-11-22 由 reprex package (v0.2.1)

关于r - 带有 bquote 的 ggplot2 facet labeller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53435686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com