1

I am trying to use qplot() to create many graphs at once. The data I would like to graph is stored in the two variables z and x in 86 separate data frames named a1 through a86. I can't seem to find a simple way to do this, as using a loop and paste(a,counter_variable,sep="") hasn't worked out for me.

For example--not sure why I can't just do:

M<-1
while(M<87){
n<-paste("a",M,sep=""))
qplot(n$z, n$x, geom='smooth')
M=M+1
}

(btw I realize there is probably a very easy solution to this)

PabloCohen
  • 11
  • 2
  • 1
    Right now this is entirely too broad. [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R question that folks can help with. That includes a sample of data, all necessary code, and a clear explanation of what you're trying to do and what hasn't worked. – camille Jun 06 '19 at 02:53
  • Why not to bind the data.frames and use ggplot facet wrap? – DJV Jun 06 '19 at 06:36

2 Answers2

0

That is actually quite an ingenious way of coding it, is it R like, not in a thousand years, the main problem with your solution is that r doesn't recognize n as your dfs n is just a string.

I am not even sure this would be the best way to do it as we could group your 87 dfs and plot them but here it is.

library(tidyverse)
df1 <- tibble(a = sample(1:100,100),b = sample(1:100,100))
df2 <- tibble(a = sample(1:100,100),b = sample(1:100,100))

M <- 1
while(M<3){
n <- paste("df",M,sep = "")  
n <- get(n)
Sys.sleep(0)
i <- qplot(n[["a"]],n[["b"]],geom = "smooth")
Sys.sleep(0)
print(i)
M = M +1
}
Bruno
  • 4,109
  • 1
  • 9
  • 27
0

This is a classic reason to use one list of similarly structured objects and not many separate objects, flooding your global environment and requiring extensive maintenance and search/recall. When objects are saved to a list you can run consistent operations on the elements like individual qplots or one large qplot.

Below are equivalent calls to save all data frames into a single, named list but as @Gregor advises: avoid having a bunch of data.frames not in a list ... don't wait until you have a bunch of a data.frames to add them to a list. Start with the list.

df_list <- Filter(is.data.frame, eapply(.GlobalEnv, identity))

df_list <- Filter(is.data.frame, mget(x=ls(), envir=.GlobalEnv))

From there, use lapply for individual plots or concatenate all data frames into a single data frame with indicator field and run facets.

# INDIVIDUAL PLOTS ------------------------------------------------
lapply(df_list, function(df) qplot(df$z, df$x, geom='smooth'))


# SINGLE PLOT -----------------------------------------------------
# ADD INDICATOR COLUMN IN EACH DF
df_list <- Map(function(df, nm) transform(df, a_name = nm), df_list, names(df_list))

# CONCATENATE ALL DF ELEMENTS
final_df <- do.call(rbind, unname(df_list))

# QPLOT WITH FACETS
with(final_df, qplot(z, x, geom='smooth', facets= ~ num))

To demonstrate with random, seeded data of nine data frames:

set.seed(662019)
a_names <- paste0("a", 1:9)

# BUILD DF LIST EACH WITH INDICATOR COLUMN
df_list <- lapply(a_names, function(i) data.frame(a_name = i, x = runif(100), z = runif(100)))

# CONCATENATE ALL DF ELEMENTS
final_df <- do.call(rbind, unname(df_list))

# QPLOT WITH FACETS
with(final_df, qplot(z, x, geom='smooth', facets= ~ a_name))

Plot Output

Parfait
  • 104,375
  • 17
  • 94
  • 125