[R] storing ggplot objects as components of a vector

Uwe Ligges ligges at statistik.tu-dortmund.de
Fri Feb 15 10:15:22 CET 2008



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)<-3

You 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]<-P

and 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.



More information about the R-help mailing list