2

I tried to generate graphs using a loop for the first time. I have to generate double y-axis graphs for 400 species. Since they had to be double-y-axis graphs, I did not use ggplot2. I manage to see the graphs in Rstudio, but when I try to open the PDF (I also tried TIFF,JPEG,...) they cannot be opened. (Free translation of the error code: cannot be opened since this type of file is not supported or the file is damaged)

I read a lot of posts on ending your code with dev.off() but this does not help. this is the code. A single file generated with the same code (but without a loop) opens fine...

for (i in categories){
  plot_data<-subset(af, nummer_soort_naam == i)
  par(mar = c(5,5,2,5))
  with(plot_data,
       plot(jaar,
            aantal_waargenomen_individuen_per_duizend_daghokbezoeken, 
            type="l"))# pch=16))}
  par(new = T)
  with(plot_data, 
       plot(jaar, ruwe_aantal_ind_waargenomen, 
            pch=16, axes=F, xlab=NA, ylab=NA, cex=1.2,
            main=paste(i,sep=" ")))
  axis(side = 4)
  mtext(side = 4, line = 3, 'ruwe aantal individuen waargenomen')
  legend("topleft", 
         legend=c("gecorrigeerd aantal","ruwe aantallen"),
         lty=c(1,0), 
         pch=c(NA, 16), 
         col=c("black", "black"))
  dev.print(file=paste(i,".pdf",sep=""))
  dev.off()
}

The data looks like this:

soort_id nummer_soort_naam jaar ruwe_aantal_ind_waargenomen aantal_waargenomen_individuen_per_duizend_daghokbezoeken
1 1_Roerdomp_Botaurus_stellaris 1990 6 3.84
1 1_Roerdomp_Botaurus_stellaris 1991 12 7.89
1 1_Roerdomp_Botaurus_stellaris 1992 10 6.9
1 1_Roerdomp_Botaurus_stellaris 1993 7 3.7
1 1_Roerdomp_Botaurus_stellaris 1994 37 20.19
2 2_Dodaars_Tachybaptus_ruficollis 1990 399 248.6
2 2_Dodaars_Tachybaptus_ruficollis 1991 569 365.9
2 2_Dodaars_Tachybaptus_ruficollis 1992 560 376.1
2 2_Dodaars_Tachybaptus_ruficollis 1993 312 163
2 2_Dodaars_Tachybaptus_ruficollis 1994 373 198.3

Can somebody help me based on this code and see the error?

Thank you very much in advance!

Kristijn

Kristijn
  • 21
  • 4
  • Does it work when you run it without the loop, i.e only on a single point? Besideds thatg, you should include data as well so that your scenario can be recreated. It's difficult to help you otherwise. – ErrantBard Jun 02 '17 at 11:12
  • It works indeed when using only a single species (and no loop). I added a small part of the data in the question – Kristijn Jun 02 '17 at 12:02
  • As far as I can tell there seems to be some kind of error in generated pdf-file. Why it is I can't tell, perhaps some kind of bug in dev.print? I would suggest try ggplot and ggsave instead, that seems to have helped this user: https://stackoverflow.com/a/13429099/3977995 – ErrantBard Jun 02 '17 at 12:31
  • Thank you for the suggestion. But ggplot is not very suited for two Y-axes in a loop because you have to scale the data for the second Y-axis (if I understood correctly). – Kristijn Jun 02 '17 at 13:51
  • I think this might be what you want:[Print to PDF in a for loop](https://stackoverflow.com/questions/5882204/print-to-pdf-in-a-for-loop?rq=1). – YQ.Wang Nov 23 '17 at 08:33

0 Answers0