[R] ggplot2: proper use of facet_grid inside a function
Bryan Hanson
hanson at depauw.edu
Fri Oct 2 17:20:49 CEST 2009
Hello Again R Folk:
I have found items about this in the archives, but I’m still not getting
it right. I want to use ggplot2 with facet_grid inside a function with
user specified variables, for instance:
p <- ggplot(data, aes_string(x = fac1, y = res)) + facet_grid(. ~
fac2)
Where data, fac1, fac2 and res are arguments to the function. I have
tried
p <- ggplot(data, aes_string(x = fac1, y = res)) + facet_grid(. ~
as.name(fac2))
and
p <- ggplot(data, aes_string(x = fac1, y = res)) + facet_grid(“. ~
fac2”)
But all of these produce the same error:
Error in `[.data.frame`(plot$data, , setdiff(cond, names(df)), drop =
FALSE) :
undefined columns selected
If I hardwire the true identity of fac2 into the function, it works as
desired, so I know this is a problem of connecting the name with the
proper value.
I'm up to date on everything:
R version 2.9.2 (2009-08-24)
i386-apple-darwin8.11.1
locale:
en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] grid datasets tools utils stats graphics
grDevices methods
[9] base
other attached packages:
[1] Hmisc_3.6-0 ggplot2_0.8.3 reshape_0.8.3
proto_0.3-8
[5] mvbutils_2.2.0 ChemoSpec_1.1 lattice_0.17-25
mvoutlier_1.4
[9] plyr_0.1.8 RColorBrewer_1.0-2 chemometrics_0.4 som_0.3-4
[13] robustbase_0.4-5 rpart_3.1-45 pls_2.1-0 pcaPP_1.7
[17] mvtnorm_0.9-7 nnet_7.2-48 mclust_3.2
MASS_7.2-48
[21] lars_0.9-7 e1071_1.5-19 class_7.2-48
loaded via a namespace (and not attached):
[1] cluster_1.12.0
Thanks for any help! Bryan
*************
Bryan Hanson
Professor of Chemistry & Biochemistry
DePauw University, Greencastle IN USA
More information about the R-help
mailing list