Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot lines in ggplot from a list of dataframes

Tags:

list

plot

r

ggplot2

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!

like image 206
Lucas Spangher Avatar asked Feb 14 '17 15:02

Lucas Spangher


People also ask

How do I plot a DataFrame from a list in R?

The geom_line() method is used to plot the data in the form of lines. We will be using the lapply() method in base R which applies a function, which may be user defined or pre-defined to the input data object. Parameter : list-of-data-frame – The list of dataframes formed.

How do I plot multiple data frames in R?

To plot multiple datasets, we first draw a graph with a single dataset using the plot() function. Then we add the second data set using the points() or lines() function.

What does %>% do in ggplot?

%>% is a pipe operator reexported from the magrittr package. Start by reading the vignette. Adding things to a ggplot changes the object that gets created. The print method of ggplot draws an appropriate plot depending upon the contents of the variable.

Does ggplot work with DataFrame?

The dataframe is the first parameter in a ggplot call and, if you like, you can use the parameter definition with that call (e.g., data = dataframe ). Aesthetics are defined within an aes function call that typically is used within the ggplot call.


1 Answers

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.

like image 181
eipi10 Avatar answered Sep 24 '22 17:09

eipi10