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