JDS - 1 month ago 15x
R Question

# Using lapply to make a plot with factors from a list of data frames

I generated a data frame with two factors, an x-variable, and a y-variable:

``````set.seed(1)
abc.df <- data.frame(col1 = rep(c("a", "b", "c"), 1000), col2 = rep(1:4, 750),
col3 = rnorm(3000), col4 = rnorm(3000, 2))
names(abc.df) <- c("factor1", "factor2", "q", "value")
abc.df\$factor1 <- as.factor(abc.df\$factor1)
abc.df\$factor2 <- as.factor(abc.df\$factor2)
abc_list <- split(abc.df, abc.df\$factor1)
``````

I want to generate a three plots for each letter (a, b, and c) with the number factors distinguished by color, but it generated an error message:

``````par(mfrow = c(1, 3))
lapply(abc_list, function(x) {plot(abc_list[[x]]\$q, abc_list[[x]]\$value,
col = factor2)})
Error in abc_list[[x]] : invalid subscript type 'list'
``````

How can I make the series of plots with the right syntax?

When you use `lapply` to loop through a list, the argument passed to the function is the element of the list not the index, so you can not use `abc_list[[x]]` to subset instead, just use `x`, so for your case:

``````par(mfrow = c(1, 3))
lapply(abc_list, function(x) {plot(x\$q, x\$value, col = x\$factor2)})
``````

There is another convenient way to get this kind of plot using `ggplot2` package with `facet_grid`, where you don't need to split your original data frame into lists but specify the `factor1` as a variable to layout your plots:

``````library(ggplot2)
ggplot(abc.df, aes(x = q, y = value, col = factor2)) + geom_point() + facet_grid(.~factor1)
``````