【问题标题】:Making plot functions with ggplot and aes_string用 ggplot 和 aes_string 制作绘图函数
【发布时间】:2012-04-04 04:16:31
【问题描述】:

在 Hadley Wickham 的 ggplot2 书中第 10.3 章中,他提到了制作绘图函数。我想制作许多使用刻面的类似图,但我无法引用列。如果我所有的参考都是美学的,那么我可以使用 aes_string 并且一切正常。 Facet_wrap 似乎没有类似物。

library(ggplot2)
data(iris)

这是我想要功能化的情节。

pl.flower1 <- ggplot(data=iris, 
                    aes_string(x='Sepal.Length', y='Sepal.Width', color='Petal.Length')) +
                                 geom_point() +facet_wrap(~Species)

如果我不分面,这可行。

flowerPlot <- function(dat, sl, sw, pl, sp){
  ggplot(data=dat, aes_string(x=sl, y=sw, color=pl)) + geom_point()
}
pl.flower2 <- flowerPlot(iris, sl='Sepal.Length', sw='Sepal.Width', pl='Petal.Length')

“sp”下面两行应该是什么?一个公式,一个字符串?也许整个方法都是错误的。

flowerPlotWrap <- function(dat, sl, sw, pl, sp){
      ggplot(data=dat, aes_string(x=sl, y=sw, color=pl)) + geom_point() +facet_wrap(sp)
    }
    pl.flower3 <- flowerPlotWrap(iris, sl='Sepal.Length', sw='Sepal.Width', pl='Petal.Length', sp= ?????)

除了答案之外,我还想知道有人如何解决这个问题?

【问题讨论】:

标签: r ggplot2


【解决方案1】:

facet_wrap 需要一个公式作为它的第一个参数,所以我只需用 as.formula 强制它,然后将我的 sp 作为字符串输入:

flowerPlotWrap <- function(dat, sl, sw, pl, sp){
      ggplot(data=dat, aes_string(x=sl, y=sw, color=pl)) + 
      geom_point() +facet_wrap(as.formula(sp)) # note the as.formula
}
pl.flower3 <- flowerPlotWrap(iris, sl='Sepal.Length', 
                             sw='Sepal.Width', pl='Petal.Length', 
                             sp= '~Species')

或者,如果我的公式总是看起来像 ~[columnname],我可以将其构建到 flowerPlotWrap 并传入列名:

flowerPlotWrap <- function(dat, sl, sw, pl, sp){
      ggplot(data=dat, aes_string(x=sl, y=sw, color=pl)) + 
      geom_point() +facet_wrap(as.formula(sprintf('~%s',sp)))
}
pl.flower3 <- flowerPlotWrap(iris, sl='Sepal.Length', 
                             sw='Sepal.Width', pl='Petal.Length', 
                             sp= 'Species')

(对您问题中可重复的示例表示敬意!如果每个人都提出问题并且他们会更快地得到答案)。

【讨论】:

  • 感谢您的明确答复。你是怎么知道 facet_wrap 需要一个公式的?
  • 如果您查看?facet_wrap,它会显示facet_wrap(facets,...)facets: formula specifying variabls to facet by
【解决方案2】:

这里有一些使用来自ggplot2 V3.0.0 的新功能的替代方案

使用字符串:

flowerPlot <- function(dat, sl, sw, pl, sp){
  ggplot(data=dat, aes(x=!!ensym(sl), y=!!ensym(sw), color=!!ensym(pl))) + 
    geom_point() +
    facet_wrap(eval(expr(~!!ensym(sp))))
}

flowerPlot(iris, sl='Sepal.Length', sw='Sepal.Width', pl='Petal.Length', sp = 'Species')

使用名称:

flowerPlot2 <- function(dat, sl, sw, pl, sp){
  ggplot(data=dat, aes(x=!!enquo(sl), y=!!enquo(sw), color=!!enquo(pl))) + 
    geom_point() +
    facet_wrap(eval(expr(~!!enquo(sp))))
}

flowerPlot2(iris, sl= Sepal.Length, sw=Sepal.Width, pl=Petal.Length, sp = Species)

【讨论】:

    【解决方案3】:

    如果我只使用sp='Species',您的函数在未经修改的情况下对我来说工作得很好,即您想要分面的变量的名称。

    flowerPlotWrap(iris, sl='Sepal.Length', sw='Sepal.Width', pl='Petal.Length', sp='Species')

    【讨论】:

      猜你喜欢
      • 2014-06-17
      • 2021-01-22
      • 2019-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多