ggplot2 - importing and plotting multiple .csv files
我尝试了批量导入,但是我认为ggplot2需要数据帧,而我只能列出一个元素列表。我在ggplot2中设置了一个简单的代码,该代码从多个csv文件导入数据并覆盖其趋势线。所有.csv文件都位于同一文件夹中,并且具有相同的格式。有没有一种方法可以从文件夹中导入所有.csv文件并将其全部绘制在ggplot中,而无需复制此代码数百次?
谢谢您的帮助!
1 2 3 4 5 6 7 8 9 10 11 | 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) |
这很有趣。我正在使用
由于要在一个图中绘制所有数据,因此将所有数据放入一个数据帧是有意义的。函数
1 2 3 4 5 6 7 8 9 10 | 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() |
一些解释
前两行创建一个命名向量,其元素是csv文件的完整路径,并且该向量的名称是文件名。这使得使用