【问题标题】:Key-value mapping of axis/variable labels in ggplotggplot中轴/变量标签的键值映射
【发布时间】:2018-07-09 03:58:28
【问题描述】:

我经常使用具有“R 友好”/“程序员友好”列名的数据框,通常没有空格和/或缩写(在进行分析时懒得输入全名)。例如:

ir <- data.frame(
   sp=iris$Species,
   sep.len=iris$Sepal.Length,
   sep.wid=iris$Sepal.Width,
   pet.len=iris$Petal.Length,
   pet.wid=iris$Petal.Width
)

当我用 ggplot 绘制这些时,我经常想用“用户友好”的列名替换标签,例如

p <- ggplot(ir, aes(x=sep.len, y=sep.wid, col=sp)) + geom_point() +
  xlab("sepal length") + ylab("sepal width") + 
  scale_color_discrete("species")

问题:有没有办法指定标签映射传递给 ggplot ?

lazy.labels <- c(
  sp     ='species',
  sep.len='sepal length',
  sep.wid='sepal width',
  pet.len='petal length',
  pet.wid='petal width'
)

然后做类似的事情

p + labs(lazy.labels)

甚至

p + xlab(lazy.labels[..x..]) + ylab(lazy.labels[..y..])

..x....y.. 是一些自动 ggplot 变量,其中包含当前 X/Y 变量的名称? (然后我可以将这些注释放入一个方便的函数中,而不必为每个图形更改它们)

当我在报表中绘制许多图时,这特别有用。我总是可以用“用户友好”列重命名ir,但是我必须做很多

ggplot(ir, aes(x=`sepal length`, y=`sepal width`, ...

因为都是空格,有点麻烦。

【问题讨论】:

  • 这是你要找的东西吗stackoverflow.com/a/49943976/786542
  • @Tung 这是一个类似但不相同的问题 - 这与保存变量名(如 x.var &lt;- 'sep.len')和让 ggplot 将变量名正确解释为 sep.len 而不是 x.var (@987654336 @ 可以为我解决这个问题)。
  • 我想将标题更改为“轴/变量标题”而不是“标签”。是的,labs() 函数暗示“标签”是一个合适的词选择,但该函数可能不是大多数用户被介绍给这个图形组件的方式。使用scale_*() 时,相关参数为name,而labels 参数则选择用于中断的标签。使用theme() 时,相关参数为axis.title.*。我认为最好使用theme() 中的术语,因为它具有一组独特的参数,对应于几乎详尽的图形组件集。

标签: r ggplot2


【解决方案1】:

我深入研究了 ggplot 对象并想出了这个:好处是您不需要提前知道映射

library(ggplot2)

ir <- data.frame(
  sp = iris$Species,
  sep.len = iris$Sepal.Length,
  sep.wid = iris$Sepal.Width,
  pet.len = iris$Petal.Length,
  pet.wid = iris$Petal.Width
)

p <- ggplot(ir, aes(x=sep.len, y=sep.wid, col=sp)) +
     geom_point() +
     scale_color_discrete("species")


## for lazy labels

lazy.labels <- c(
  sp     ='species',
  sep.len='sepal length',
  sep.wid='sepal width',
  pet.len='petal length',
  pet.wid='petal width'
)

p$labels <-lapply(p$labels,function(x){as.character(lazy.labels[x])})

或者,使用函数:

plot_with_labels <- function(p, l) {
  p$labels <- lapply(p$labels, function(x) { as.character(l[x]) } )
  return(p)
}

plot_with_labels(p, lazy.labels)

【讨论】:

  • 啊哈,p$labels!非常感谢!
  • 从 ggplot2 3.3.5 开始,上面的函数设置为带有fallback 属性的NA 标签。这修复了:plot_with_labels_mod &lt;- function(p, l) { swap &lt;- function(x) { if (is.null(attr(x, "fallback"))) { as.character(l[x]) } else { x } } p$labels &lt;- lapply(p$labels, swap) return(p) }
【解决方案2】:

如果您的图表始终相同,一种解决方案是提前创建标签,使用列表将美学映射到新名称以替换默认名称。那么你就可以 使用labs(lazy.labels)

ir <- data.frame(
  sp = iris$Species,
  sep.len = iris$Sepal.Length,
  sep.wid = iris$Sepal.Width,
  pet.len = iris$Petal.Length,
  pet.wid = iris$Petal.Width
)
library(ggplot2)
# mapping aesthetics names to labels
lazy.labels <- list(
  col = 'species',
  x = 'sepal length',
  y ='sepal width'
)
p <- ggplot(ir, aes(x = sep.len, y = sep.wid, col = sp)) + 
  geom_point() +
  labs(lazy.labels)

reprex package (v0.2.0) 于 2018 年 7 月 9 日创建。

【讨论】:

  • 谢谢!不幸的是,我事先不知道我的标签 - 我通常会制作很多图来探索不同 X、Y、颜色的各种效果
猜你喜欢
  • 2018-09-05
  • 2018-03-28
  • 1970-01-01
  • 2012-11-28
  • 1970-01-01
  • 1970-01-01
  • 2018-05-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多