6

I have a list of data.frames:

samplelist = list(a = data.frame(x = c(1:10), y=rnorm(10),
                 b= data.frame(x=c(5:10), y = rnorm(5),
                 c = data.frame(x=c(2:12), y=rnorm(10))

I'd like to structure a ggplot of the following format:

ggplot()+ 
    geom_line(data=samplelist[[1]], aes(x,y))+
    geom_line(data=samplelist[[2]], aes(x,y))+
    geom_line(data=samplelist[[3]], aes(x,y))

But that isn't super automated. Does anyone have a suggestion for how to address this?

Thanks!

1
  • 2
    FYI, your samplelist code has a few errrors. Commented Feb 14, 2017 at 15:59

4 Answers 4

12

ggplot works most efficiently with data in "long" format. In this case, that means stacking your three data frames into a single data frame with an extra column added to identify the source data frame. In that format, you need only one call to geom_line, while the new column identifying the source data frame can be used as a colour aesthetic, resulting in a different line for each source data frame. The dplyr function bind_rows allows you to stack the data frames on the fly, within the call to ggplot.

library(dplyr)
library(ggplot2)

samplelist = list(a = data.frame(x=c(1:10), y=rnorm(10)),
                  b = data.frame(x=c(5:10), y=rnorm(6)),
                  c = data.frame(x=c(2:12), y=rnorm(11)))

ggplot(bind_rows(samplelist, .id="df"), aes(x, y, colour=df)) +
  geom_line()

enter image description here

I assumed above that you would want each line to be a different color and for there to be a legend showing the color mapping. However, if, for some reason, you just want three black lines and no legend, just change colour=df to group=df.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm receiving Error in bind_rows_(x, .id) : Argument 1 can't be a list containing data frames
3

Or you could use lapply.

library(ggplot2)

samplelist = list(a = data.frame(x = c(1:10), y=rnorm(10)),
                  b= data.frame(x=c(5:10), y = rnorm(6)),
                  c = data.frame(x=c(2:12), y=rnorm(11)))

p <- ggplot()

plot <- function(df){
    p <<- p + geom_line(data=df, aes(x,y))
}

lapply(samplelist, plot)

p

1 Comment

using <<- is often not the best strategy in R (typically because one prefers to avoid side-effects in non-local environments); I think Reduce() is more idiomatic here.
3

This will work -

library(ggplot2)
samplelist <- list(a = data.frame(x = c(1:10), y=rnorm(10)),
                  b = data.frame(x=c(5:10), y = rnorm(6)),
                  c = data.frame(x=c(2:12), y=rnorm(11)))

p <- ggplot()
for (i in 1:3) p <- p + geom_line(data=samplelist[[i]], aes(x,y))
p

2 Comments

your suggestion of using i in aes() will fail
Good point, they would all need to be in the same df to use that. edited accordingly
1

Reduce is another option to add things iteratively,

library(ggplot2)

samplelist = list(a = data.frame(x = c(1:10), y=rnorm(10)),
                  b= data.frame(x=c(5:10), y = rnorm(6)),
                  c = data.frame(x=c(2:12), y=rnorm(11)))

pl <- Reduce(f = function(p, d) p + geom_line(data=d, aes(x,y)), 
             x = samplelist, init = ggplot(), accumulate = TRUE)

gridExtra::grid.arrange(grobs = pl)

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.