0

I have one table of derived vegetation indices for 63 sample sites from different satellites. this gives me a table with 63 observations(sample sites) and 56 variables(1 Sample ID, 50 vegetation indices, 4 Biomass and 1 LAI). The last 5 columns of the table are the biomass and LAI, and the first column is the sample ID. I want to generate a plot showing the relationship between a single vegetation index and one of the biomass parameters. I am able to do this using the plot function, for one observation and variable at a time.

plot(data$Dry10, data$X8047EVImea)

I don't want to run this code 50 times and again by 5 sets for each biomass and LAI parameter.

Is there a way to loop or nested loop this plot function so that I can generate 200 graphs at once?

Also, I will place a regression line in each plot to see what vegetation index will best represent the amount of biomass present at the sample site.

This is my first post on stackoverflow, so please don't hesitate to request more information on the problem if I have missed something.

lmo
  • 37,904
  • 9
  • 56
  • 69
  • See this question: http://stackoverflow.com/questions/24697419/multiple-graphs-within-plot-with-loop. Seems like basically the same thing you are asking. Also for best results, you should only ask one question at a time and always include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – MrFlick Sep 01 '15 at 20:09
  • Also maybe have a look at facets in the `ggplot2` package. This would require a lot fewer plot calls and produce much more readable bit of code. http://www.cookbook-r.com/Graphs/Facets_%28ggplot2%29/ – cr1msonB1ade Sep 01 '15 at 21:52
  • @MrFlick thanks for the comment. I found that link very useful. I have not yet managed to plot everything but I'm getting at least 4 plots up at a time. This is good enough for my purpose. Thanks! – Sean Cullen Sep 08 '15 at 16:04
  • @cr1msonB1ade I have found errors in my data. I do hope to try your method as soon as I resolve my other issues. Thanks for the help! – Sean Cullen Sep 08 '15 at 16:05

1 Answers1

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.

cr1msonB1ade
  • 1,716
  • 9
  • 14