在Hadley Wickham的第10.3章中的ggplot2一书中,他提到了制作情节功能.我想制作许多使用刻面的类似图,但我不能引用列.如果我的所有引用都是美学的,那么我可以使用aes_string,一切正常.Facet_wrap似乎没有类似的东西.
library(ggplot2)
data(iris)
Run Code Online (Sandbox Code Playgroud)
这是我想要功能化的情节.
pl.flower1 <- ggplot(data=iris,
aes_string(x='Sepal.Length', y='Sepal.Width', color='Petal.Length')) +
geom_point() +facet_wrap(~Species)
Run Code Online (Sandbox Code Playgroud)
如果我不这样做,这是有效的.
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')
Run Code Online (Sandbox Code Playgroud)
"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= ?????)
Run Code Online (Sandbox Code Playgroud)
除了答案,我会喜欢指向任何人如何解决这个问题?