I am trying to plot multiple variable correlation plots on one page - but the points aren't being plotted correctly when I use a for loop. They are fine if I create each plot individually.
I have a dataframe called sig_df with the variable names, intercept and slope of the regression line, and the p-value of the correlation:
> sig_df x y intercept slope p_value 1 Share.incentives User.profiles -0.1667891 0.5826598 0.0003577163 2 Share.incentives Translation 2.2689199 0.4790595 0.0005928465 3 Translation User.interaction 1.4696970 0.5757576 0.0008921255 and also a master dataframe with the x and y values of each variable, for a sample of 26
> master Share.incentives User.profiles Translation User.interaction 1 5 4 5 5 2 4 1 3 4 3 6 4 5 5 4 6 4 4 4 5 4 1 5 3 .. ... ... ... ... I've used a for loop to look at every row in sig_df, look up the variable names in master and make a correlation plot. I have stored these plots in a list, then use grid.arrange to plot them on a page - the ablines are plotting correctly but the points on every plot are the same as the final plot in my list! When I don't use a for loop, this doesn't happen. Does anyone know what could be causing this behaviour?
plot_list<- list() for(i in 1:nrow(sig_df)){ y <- (sig_df$y[[i]]) x <- (sig_df$x[[i]]) slope <- (sig_df$slope[[i]]) intercept <- (sig_df$intercept[i]) plot_list[[i]] <- ggplot(data=master, aes(get(x), get(y))) + geom_point() + geom_abline(slope=slope, intercept=intercept) } do.call(grid.arrange, c(plot_list, ncol=4)) Thanks, KP
df <- select(master, get(x), get(y)) colnames(df)<- c('x','y') plot_list[[i]] <- ggplot(data=df, aes(x,y)) + geom_point() + geom_abline(slope=slope, intercept=intercept)get()is generally best avoided, and definitely not recommended within a ggplot. Note that you can simplify the grid.arrange step withgrid.arrange(grobs = plot_list, ncol=4)