从单个大型数据框的列中一次绘制10个以上的图形

时间:2015-11-05 08:36:59

标签: r pdf plot ggplot2

从我做的实验中,我得到了包含时间序列数据的大文件。每列代表一个我想在图表中绘制的系列。 X轴和Y轴的范围并不重要,因为我只需要它作为概述。

问题是,每个数据框有150-300列(和400行),而且我无法弄清楚如何一次绘制超过10个图。

library(ggplot2)
library(reshape2)
csv <- read.csv(file = "CSV-File-path", header = F, sep = ";", dec = ".")[,1:10]
df <- as.data.frame(csv)
plot.ts(df)

当我将[,1:10]更改为[,1:11]时,我收到错误:

  

plotts中的错误(x = x,y = y,plot.type = plot.type,xy.labels =   xy.labels,:不能将超过10个系列作为“多个”

理想情况下,我希望输出多页PDF文件,每页至少有10个图表。我对R很新,我希望你能帮助我。

2 个答案:

答案 0 :(得分:3)

这是一个ggplot2方法:

library(ggplot2)
library(reshape2)

nrow <- 200
ncol <- 24
df <- data.frame(matrix(rnorm(nrow*ncol),nrow,ncol))

# use all columns and shorten the measure name to mvar
mdf <- melt(df,id.vars=c(),variable.name="mvar") 

gf <- ggplot(mdf,aes(value,fill=mvar)) + 
         geom_histogram(binwidth=0.1) + 
         facet_grid(mvar~.)

print(gf) # print it out so we can see it

ggsave("gplot.pdf",plot=gf)   # now save it as a PDF

这是情节的样子: enter image description here

答案 1 :(得分:2)

这是一种方法。这个列以5个为一组对列进行分组,然后将它们作为单独的页面写入单个PDF中。但如果是我,我将使用ggplot2并在单个情节中进行。

nrow <- 18
ncol <- 20
df <- data.frame(matrix(runif(nrow*ncol),nrow,ncol))

plots <- list()

ngroup <- 5
icol <- 1
while(icol<=ncol(df)){
   print(icol)
   print(length(plots))
   ecol <- min(icol+ngroup-1,ncol(df))
   plot.ts(df[,icol:ecol])
   plots[[length(plots)+1]] <- recordPlot()
   icol <- ecol+1
}
graphics.off()

pdf('plots.pdf', onefile=TRUE)
for (p in plots) {
  replayPlot(p)
}
graphics.off()