【问题标题】:ggplot2 facet labeller with bquoteggplot2 facet labeller with bquote
【发布时间】:2018-11-22 17:14:01
【问题描述】:

又一个贴标机问题...我正在努力在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 已更新。现在不推荐使用带有变量和值参数的标注器。请参阅标注器文档。”

【问题讨论】:

标签: r ggplot2


【解决方案1】:

您可以使用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()

reprex package (v0.2.1) 于 2018 年 11 月 22 日创建

【讨论】:

  • 是的,您的回答对于我的特定(和简单)示例是正确的,这就是我必须验证它的原因。不幸的是,我正在研究一个更大(更多方面)的“真实生活”数据集,每个方面都有不同类型的标注器,有些带有 bquote,有些则没有……所以,如果还有其他命题,我会很高兴。
猜你喜欢
  • 2016-05-13
  • 1970-01-01
  • 2015-08-24
  • 1970-01-01
  • 2021-11-17
  • 2014-03-19
  • 2019-01-30
  • 2019-04-12
  • 1970-01-01
相关资源
最近更新 更多