Gallego Liberman, Matias
2008-Feb-15 09:08 UTC
[R] storing ggplot objects as components of a vector
Se ha borrado un texto insertado con un juego de caracteres sin especificar... Nombre: no disponible Url: https://stat.ethz.ch/pipermail/r-help/attachments/20080215/8098356a/attachment.pl
Gallego Liberman, Matias wrote:> Hi. > > I have a lot of plots that i need to store as components of a vector. > Here you have an example > >> DF<-data.frame(A=rnorm(12),B=c(1:12),C=rep(c(1:4),each=3)) >> Q<-c() >> length(Q)<-3You need a list, hence replace the last two lines by: Q <- vector(mode="list", length=2)>> PAC<-geom_point(mapping=aes(x=A,y=B),data=DF[DF$C==1,]) >> P<-ggplot()+PAC >> P #generates the plot >> Q[1]<-Pand here replace last line by: Q[[1]]<-P Best, Uwe Ligges> Warning message: > In Q[1] <- P : > n?mero de items para para sustituir no es un m?ltiplo de la longitud del reemplazo >> Q[1] > [[1]] > NULL > > > I guess the problem is the class of Q[1]. > if so, what class should I coerce Q to for the sentence: > Q[1] to generate the plot as > P does? > > thanks in advance > > [[alternative HTML version deleted]] > > > > ------------------------------------------------------------------------ > > ______________________________________________ > 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.
ONKELINX, Thierry
2008-Feb-15 09:38 UTC
[R] storing ggplot objects as components of a vector
You'll need to store it in a list rather than a vector. library(ggplot2) DF<-data.frame(A=rnorm(12),B=c(1:12),C=rep(c(1:4),each=3)) Q <- list() PAC<-geom_point(mapping=aes(x=A,y=B),data=DF[DF$C==1,]) P<-ggplot()+PAC Q[[length(Q) + 1]] <- P Q[[1]] You could shorten this into library(ggplot2) DF<-data.frame(A=rnorm(12),B=c(1:12),C=rep(c(1:4),each=3)) Q <- list() Q[[length(Q) + 1]] <- ggplot(DF[DF$C==1,], aes(x=A,y=B)) + geom_point() Q[[1]] 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 op inbo.be www.inbo.be Do not put your faith in what statistics say until you have carefully considered what they do not say. ~William W. Watt A statistical analysis, properly conducted, is a delicate dissection of uncertainties, a surgery of suppositions. ~M.J.Moroney -----Oorspronkelijk bericht----- Van: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org] Namens Gallego Liberman, Matias Verzonden: vrijdag 15 februari 2008 10:09 Aan: r-help op r-project.org Onderwerp: [R] storing ggplot objects as components of a vector Hi. I have a lot of plots that i need to store as components of a vector. Here you have an example> DF<-data.frame(A=rnorm(12),B=c(1:12),C=rep(c(1:4),each=3)) > Q<-c() > length(Q)<-3 > PAC<-geom_point(mapping=aes(x=A,y=B),data=DF[DF$C==1,]) > P<-ggplot()+PAC > P #generates the plot > Q[1]<-PWarning message: In Q[1] <- P : n?mero de items para para sustituir no es un m?ltiplo de la longitud del reemplazo> Q[1][[1]] NULL I guess the problem is the class of Q[1]. if so, what class should I coerce Q to for the sentence: > Q[1] to generate the plot as > P does? thanks in advance [[alternative HTML version deleted]]