用ggplot和aes_string绘制绘图函数 [英] Making plot functions with ggplot and aes_string

查看:167
本文介绍了用ggplot和aes_string绘制绘图函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在第10.3章的Hadley Wickham的 ggplot2 一书中,他暗示要制作剧情功能。我想制作许多使用刻面的类似地块,但我无法引用列。如果我所有的引用都是美学的,那么我可以使用aes_string,并且一切正常。

  library(ggplot2)
数据(虹膜)
code>

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

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

如果我不方面,这是有效的。

  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 < - 函数(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 = ?????)

除了答案之外,我还会喜欢指针,指出任何人如何解决这个问题?

解决方案

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

  flowerPlotWrap < - 函数(dat,sl,sw,pl,sp){
ggplot(data = dat,aes_string(x = sl,y = sw,color = pl))+
geom_point()+ facet_wrap(as .formula(sp))#注意as.formula
}
pl.flower3< - flowerPlotWrap(iris,sl ='Sepal.Length',
sw ='Sepal.Width',pl ='Petal.Length',
sp ='〜Species')

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

$ p $ flowerPlotWrap < - 函数(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')

(对您问题中的可重复示例!如果每个人都问问题以及他们会更快得到答案)。

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.

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.

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?

解决方案

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:

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:

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

这篇关于用ggplot和aes_string绘制绘图函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆