0

I have a large data set (~140000 values) that I would like to make plots for. The data is composed of a value, a time stamp and an ID number. The ID number uses the time stamp to bin the values into a time duration (ex. every 10 minutes, 1 hour,etc...). Each value timestamped within the first 10 minutes is assigned a 1. This continues until all values are assigned a group number. This gives me over a thousand unique group ID numbers.
The plot is a histogram with the x axis having value ranges (ex. 1:2,2:3,3:4,etc..). The Y axis is the proportion of the values sampled that fall into each range. The code I have been using to generate a plot for all the data look like this:
ggplot(ICICLICKS1, aes(x=ICIms))+ geom_histogram(aes(y=..count../sum(..count..)),binwidth=2) + scale_x_continuous(limits=c(1.5,140), breaks = seq(0,140,5))+ scale_y_continuous(limits=c(0.0,0.25))+ geom_vline(xintercept=c(10,50))+ xlab("ICI(ms)")+ ylab("Proportion")+ ggtitle("Histogram of Porpoise Inter-click Intervals")

My end goal is to have a plot for each 10 minute period. Since that would be a few thousand plots I need to find a way to get R to run the code, plotting all values with ID number 1, export plot, plot all values with ID number 2, export plot, etc... until all the ID numbers have been run. Is there a way to write a loop or some other piece of code t accomplish this?

Splishchh
  • 3
  • 3
  • [This question](http://stackoverflow.com/questions/29034863/apply-a-ggplot-function-per-group-with-dplyr-and-set-title-per-group) may get you started making a separate plot per group. – aosmith Jul 30 '15 at 19:06
  • Seems like you just want to wrap your code in a loop using `subset` on ICICLICKS1. – johnson-shuffle Aug 01 '15 at 19:35
  • if we call your plot `p`, you could do `ggsave("all.pdf", marrangeGrob(dlply(ICICLICKS1, "ID", "%+%", e1 = p), ncol=1, nrow=1))`. Admittedly not fully transparent, but concise. – baptiste Aug 03 '15 at 19:50

1 Answers1

0

As suggested by jonhson_shuffle, this will make a thousand graphs for you:

for(i in 1:max(ICICLICKS1$ID)) { 
  ggplot(subset(ICICLICKS1,ID==i), aes(x=ICIms))+
    geom_histogram(aes(y=..count../sum(..count..)),binwidth=2) +
    scale_x_continuous(limits=c(1.5,140), breaks = seq(0,140,5))+
    scale_y_continuous(limits=c(0.0,0.25))+
    geom_vline(xintercept=c(10,50))+
    xlab("ICI(ms)")+
    ylab("Proportion")+
    ggtitle("Histogram of Porpoise Inter-click Intervals")
  ggsave(paste("plot",i,".pdf"))
}

Personally i wouldn't want to go through a thousand graphs, so i group them per hour and use facet_wrap(~ID) to display them per 10 minute interval (6 graphs for every hour). Or group them for day and night. Looking at your subject that might be interesting ;-).

RHA
  • 3,677
  • 4
  • 25
  • 48