将每列的关系绘制到表格中的单个列

时间:2015-09-01 20:06:54

标签: r plot

我有一张来自不同卫星的63个样本点的衍生植被指数表。这给了我一张包含63个观测值(样本点)和56个变量(1个样本ID,50个植被指数,4个生物量和1个LAI)的表格。表格的最后5列是生物量和LAI,第一列是样本ID。 我想生成一个图表,显示单个植被指数与其中一个生物量参数之间的关系。 我能够使用绘图功能,一次一个观察和变量。

plot(data$Dry10, data$X8047EVImea)

对于每个生物量和LAI参数,我不想再次运行此代码50次。

有没有办法循环或嵌套循环这个绘图函数,这样我就可以一次生成200个图形?

此外,我将在每个图中放置一条回归线,以查看哪些植被指数最能代表样本点的生物量。

这是我关于stackoverflow的第一篇文章,所以如果我错过了什么,请不要犹豫,请求更多关于这个问题的信息。

1 个答案:

答案 0 :(得分:0)

As noted in my comment you can accomplish this with a faceted plot in the ggplot2 package. This does require a little bit of data re-arrangement that can be accomplished with the reshape2 package. Here is some code that will be close to what you want to do but since I don't completely know your data formats it might take some fixes:

library(ggplot2)
library(reshape2)
library(dplyr)

vegDat <- data[,2:51]
bioDat <- data[,52:55]
## melt the data.frames so the biomass and vegetation headers are now variables
vegDatM <- melt(vegDat, variable.name='vegInd', value.name='vegVal')
bioDatM <- melt(bioDat, variable.name='bioInd', value.name='bioVal')
## Join these datasets to create all comparisons to be made
gdat <- bind_cols(vegDatM[rep(seq_len(nrow(vegDatM)), each=nrow(bioDatM)),],
    bioDatM[rep(seq_len(nrow(bioDatM)), nrow(vegDatM)),])
## plot the data in a faceted grid
ggplot(gdat) + geom_point(aes(x=vegVal, y=bioVal)) + facet_grid(vegInd ~ bioInd)

Note that since there are 50 plots you may want to open a divice with a large height (or width if you swap the facet) i.e. pdf('foo.pdf', heigth=20). Hope this gets you on the right track.