Bryan Hanson
2009-Oct-02 15:20 UTC
[R] ggplot2: proper use of facet_grid inside a function
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
ONKELINX, Thierry
2009-Oct-05 08:12 UTC
[R] ggplot2: proper use of facet_grid inside a function
Dear Bryan, In the ggplot() function you can choose between aes() and aes_string(). In the first you need to hardwire the variable names, in the latter you can use objects which contain the variable names. So in your case you need aes_string(). Unfortunatly, facet_grid() works like aes() and not like aes_string(). That is why you are getting errors. A workaround would be to add a dummy column to your data. library(ggplot2) data <- mpg fac1 <- "cty" fac2 <- "drv" res <- "displ" data$dummy <- data[, fac2] ggplot(data, aes_string(x = fac1, y = res)) + geom_point() + facet_grid(.~dummy) HTH, Thierry ------------------------------------------------------------------------ ---- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx at inbo.be www.inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey -----Oorspronkelijk bericht----- Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Namens Bryan Hanson Verzonden: vrijdag 2 oktober 2009 17:21 Aan: <R Help Onderwerp: [R] ggplot2: proper use of facet_grid inside a function 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 ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Druk dit bericht a.u.b. niet onnodig af. Please do not print this message unnecessarily. Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document. The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document.