ggplot2 - Store static ggplot to variable as value in R -
i'm creating multiple ggplots in loop, storing them in list. problem "data" parameter pass ggplot changing on each iteration, , therefore when later print plots, printed last revision of "data" dataframe.
what do, save ggplot statically variable (by value) in way not reevaluate "data" uppon printing. ideas?
for example, if each every ggplot in list, use last revision of 'scalebounds' (from last iteration) 'data' of ggplot.
my code:
scaleplotlist <- list() (i in 1:number_of_scales) { plot.new() row <- df[i,] min <- row$min max <- row$max <- max(min(row$your_business,max),min) pinseq <- seq(from=min,to=max,length.out=6) scalebounds <-data.frame(x=c(pinseq[1],pinseq[2],pinseq[2],pinseq[1],pinseq[2],pinseq[3],pinseq[3],pinseq[2],pinseq[3],pinseq[4],pinseq[4], pinseq[3],pinseq[4],pinseq[5],pinseq[5],pinseq[4],pinseq[5], pinseq[6], pinseq[6], pinseq[5]), y=c( 0,0,1,1, 0,0,1,1, 0,0,1,1, 0,0,1,1, 0,0,1,1 ), t=c( 1,1,1,1, 2,2,2,2, 3,3,3,3, 4,4,4,4, 5,5,5,5 )) currentplot <- ggplot() + geom_polygon(data=scalebounds, mapping=aes(x=x,y=y,group=t,fill=-t)) + scale_colour_brewer() + labs(x="",y="") + ylim(0,1.5) + geom_segment(aes(x=you,y=0,xend=you,yend=1.2), colour="#3d3d3d", size=3, lineend = "round") + geom_text(label="min", aes(x=pinseq[1], y=0), size=5, colour="#fcfcfc", fontface="bold", hjust=-0.1, vjust=-.7) + geom_text(label="max", aes(x=pinseq[6], y=0), size=5, colour="#fafafa", fontface="bold", hjust=1.1, vjust=-.7) + geom_text(label="you", aes(x=you, y=.5), size=8, colour="#fafafa", fontface="bold", hjust=-0.1) + theme_nothing() scaleplotlist[[length(scaleplotlist)+1]] <- currentplot }
Comments
Post a Comment