jmd
jmd

Reputation: 13

ggplot2 - importing and plotting multiple .csv files

I have tried batch importing, but I think ggplot2 requires data frames and I have only been able to make a list of elements. I have set up a simple code in ggplot2 that imports data from multiple csv files and overlays their trendlines. All of the .csv files are in the same folder and have the same format. Is there a way to import all of the .csv files from the folder and plot all of them in ggplot without copying this code hundreds of times?

Thank you for your help!

library(ggplot2)
points1 <- read.csv("http://drive.google.com")[1:10,1:2]
points2 <- read.csv("http://drive.google.com")[1:10,1:2]
g <- (ggplot(points1, aes(x=ALPHA, y=BETA))
+labs(title="Model Run", subtitle="run4", y="LabelY", x="LabelX", caption="run4")
+ coord_cartesian(xlim=c(0,10), ylim=c(0,11)) 
#+ geom_point(data = points1)#
+geom_smooth(method="loess", span=.8, data = points1, se=FALSE)
#+ geom_point(data = points2)#
+geom_smooth(method="loess", span=.8, data = points2, se=FALSE))
plot(g)

Upvotes: 1

Views: 2272

Answers (1)

Jannik Buhr
Jannik Buhr

Reputation: 1937

This is a fun one. I am using some packages from the tidyverse (ggplot, purrr, readr) to make it more consistent.

Since you want to plot all the data in one plot, it makes sense to put all of it into one dataframe. The function purrr::map_df is perfect for this.

library(tidyverse)

files <- list.files("data/", "*.csv", full.names = T)
names(files) <- list.files("data/", "*.csv")

df <- map_df(files, ~read_csv(.), .id = "origin")

df %>% ggplot()+
    aes(x,y, color = origin)+
    geom_point()

A few explainations

The first two lines create a named vector with its elements being the full paths to the csv-files and the names of this vector being the filenames. This makes is easier to use the .id argument of map_df, which creates an additional column namend "origin" from the filenames. The notation inside map might seem a little weird at first, you could also supply a function written elesewhere to apply to each element but the ~ symbol is pretty handy: it creates a function right there and this function always takes the argument . as the current element of the vector or list you are iterating over.

Upvotes: 2

Related Questions