r - Creating multiple plots in ggplot with different Y-axis values using a loop -


i trying create multiple scatter plot graphs in ggplot have same structure different y-value. need them separate (and therefore not use facet_wrap) because in later step use grid_arrange arrange different combinations of graphs onto single layout.

because of this, need create new names each plot reflect y-value being plotted. below sample code, month variable on x-axis , want 3 separate plots of month vs. 3 additional variables (lag1_var, lag3_var , lag9_var).

df <- data.frame (month= c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12),                  lag1_var=  c (10, 20, 30, 40, 10, 40, 30, 50, 70, 90, 100, 100),                 lag3_var= c(90, 70, 50, 40, 70, 50, 20, 50, 70, 90, 10, 10),                 lag9_var = c(50, 20,90, 100, 90, 10, 40, 90, 100, 20, 30, 70)) 

my approach create list of values differ between y-values , loop on list below:

loop.list <- c("1", "3", "9")  (val in loop.list) {    yval<- paste0("lag", val, "_var")    ptitle <-paste0("graph plot lag", val, "_var")    assign(paste0("plot", val), ggplot(data=df, aes(x=month, y=get(yval)))   +geom_point(color="red", size=2) + ggtitle(ptitle))      } 

when this, 3 plots 3 different names (plot1, plot3, plot9) , correct titles (so plot 1 has title "graph plot lag1" , plot 3 has title "graph plot lag3", etc.), identical plots. loop working plot name , plot title, not y-value. outputs values last loop (for variable lag9_var).

i cannot figure out why happening, , why happens y-value , not title or plot name. have programmed in sas , new r, think approaching sas prospective instead of thinking in "r" way.

note: in code above create objects "yval" , "ptitle" outside of ggplot statement, troubleshoot. same thing happens if include them in ggplot statement below:

 (val in loop.list) {        assign(paste0("plot", val), ggplot(data=df,aes(x=month,y=get(paste0("lag", val, "_var")))) +       geom_point(color="red", size=2) +       ggtitle(paste0("graph plot lag", val, "_var")))          } 

thank help!

i think problem you're having might ggplot trying rebuild each plot when call show it, , retrieving data last reference given, rather reference given when each plot created. don't understand it, great if else can illuminate subject.

either way, following reasoning, tried separating data each plot own data frame, , seem have gotten working:

library(data.table) library(ggplot2) loop.list <- c("1", "3", "9") (val in loop.list) {     col <- grep( paste0("lag", val, "_var"), colnames(df) )     yval <- df[,c(1,col)]     setnames( yval, c( "month", "var" ) )     frameval <- paste0("frame", val)     assign( paste0("frame", val), yval )     ptitle <-paste0("graph plot lag", val, "_var")      plotval <- ggplot( data = get(frameval), aes(x=month,y=var) ) +            geom_point( color="red", size=2) +                ggtitle(ptitle)     assign( paste0("plot",val), plotval ) } 

notice grep call finding column number use plot, separating column out rest own data frame.

i can't explain why ggplot doesn't work method you've used, seems workaround, hope helps.


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -