ggplot中轴/变量标签的键值映射

8

我经常使用具有“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`, ...

由于所有的空格,这有点繁琐。


这是您正在寻找的内容吗?https://dev59.com/zarka4cB1Zd3GeqPi8fA#49943976 - Tung
还有一些:https://dev59.com/p6zka4cB1Zd3GeqP50W7#50930640 和 https://stackoverflow.com/a/50522928/786542 - Tung
@Tung 这是一个类似但不同的问题 - 这与保存变量名 x.var <- 'sep.len' 有关,让 ggplot 正确解释变量名为 sep.len 而不是 x.varaes_string 可以解决这个问题)。 - mathematical.coffee
我想将标题更改为“轴/变量标题”,而不是“标签”。是的,labs()函数暗示“标签”是一个合适的词选择,但是该函数可能不是大多数用户介绍这种图形组件的方式。当使用scale_*()时,相关参数是name,而labels参数则选择断点的标签。当使用theme()时,相关参数是axis.title.*。我认为最好使用theme()中的术语,因为它具有一组唯一的参数,对应于一个几乎耗尽的图形组件集合。 - randy
2个回答

6

我深入研究了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!非常感谢! - mathematical.coffee
从ggplot2 3.3.5开始,上述函数将使用fallback属性将标签设置为NA。这个修复方法是:`plot_with_labels_mod <- function(p, l) { swap <- function(x) { if (is.null(attr(x, "fallback"))) { as.character(l[x]) } else { x } } p$labels <- lapply(p$labels, swap) return(p) }` - tiptoebull

1
如果您的图表始终相同,一种解决方案是提前创建标签,使用将美学映射到新名称以替换默认名称的列表。然后,您可以使用 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包 (v0.2.0)于2018-07-09创建的。


谢谢!不幸的是,我事先不知道我的标签 - 我通常会制作很多图来探索不同X、Y、颜色的各种效果。 - mathematical.coffee

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接