使用ggplot和aes_string创建绘图函数

时间:2022-09-14 14:58:54

In Hadley Wickham's ggplot2 book in chapter 10.3, he alludes to making plot functions. I want to make many similar plots that use faceting, but I cannot refer to a column. If all my references are in aesthetics then I can use aes_string and everything works. Facet_wrap seems not to have an analogue.

在Hadley Wickham的第10.3章中的ggplot2一书中,他提到了制作情节功能。我想制作许多使用刻面的类似图,但我不能引用列。如果我的所有引用都是美学的,那么我可以使用aes_string,一切正常。 Facet_wrap似乎没有类似的东西。

library(ggplot2)
data(iris)

This is the plot I want to functionalize.

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

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

This works if I do not facet.

如果我不这样做,这是有效的。

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

What should "sp" be two lines below? A formula, a string? Maybe the whole aproach is wrong.

“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= ?????)

In addition to an answer I would love pointer on how anyone approaches this problem?

除了答案,我会喜欢指向任何人如何解决这个问题?

2 个解决方案

#1


15  

facet_wrap expects a formula as its first argument, so I'd just coerce it with as.formula, and feed in my sp as a string:

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

Alternatively if my formula was always going to look like ~[columnname], I could just build that in to flowerPlotWrap and pass in the column name:

或者,如果我的公式总是看起来像〜[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')

(kudos to the reproducible example in your question! If everyone asked questions as well as that they'd get answers much quicker).

(对你问题中可重复的例子感到荣幸!如果每个人都提出问题以及他们会更快地得到答案)。

#2


1  

Your function worked fine for me unmodified if I just used sp='Species', i.e. the name of the variable you want to facet by.

如果我只使用sp ='Species',即你想要面对的变量的名称,你的函数对我来说没有修改。

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

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

使用ggplot和aes_string创建绘图函数

#1


15  

facet_wrap expects a formula as its first argument, so I'd just coerce it with as.formula, and feed in my sp as a string:

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

Alternatively if my formula was always going to look like ~[columnname], I could just build that in to flowerPlotWrap and pass in the column name:

或者,如果我的公式总是看起来像〜[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')

(kudos to the reproducible example in your question! If everyone asked questions as well as that they'd get answers much quicker).

(对你问题中可重复的例子感到荣幸!如果每个人都提出问题以及他们会更快地得到答案)。

#2


1  

Your function worked fine for me unmodified if I just used sp='Species', i.e. the name of the variable you want to facet by.

如果我只使用sp ='Species',即你想要面对的变量的名称,你的函数对我来说没有修改。

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

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

使用ggplot和aes_string创建绘图函数