用ggplot和aes_string绘制绘图函数 [英] Making plot functions with ggplot and 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')
或者,如果我的公式总是看起来像 (对您问题中的可重复示例!如果每个人都问问题以及他们会更快得到答案)。 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. This is the plot I want to functionalize. This works if I do not facet. What should "sp" be two lines below? A formula, a string? Maybe the whole aproach is wrong. In addition to an answer I would love pointer on how anyone approaches this problem? Alternatively if my formula was always going to look like (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屋!〜[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')
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')
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= ?????)
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')
~[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')