0

I have a data frame with 500 species and 25,000 point locality observations of the various species. I would like to make individual species point maps, each point on the map is an occurrence of the species. Because there are so many maps to make, I need to do this in a loop. This is what I have so far.

#loop for making species map 1.1
species <- levels(raw$SpCode)
for(i in 1:length(species))  
            {
            #open the file for writing
             pdf(species[i], file=".pdf", width=5, height=4)
             plot (wrld_simpl, xlim=c(-100,-55), ylim=c(23,63), axes=TRUE, col='light grey')
             box() #adds box around map
             title(main=species[i]) #adds main title to map which should be the species name associated with the data
             points(raw, species[i]$longitude, species[i]$latittude, col='black', pch=21, bg="red", cex=0.85)
             dev.off()
             }

The main error output I am getting is:

“Error in species[i]$longitude : $ operator is invalid for atomic vectors
In addition: Warning message:
‘mode(onefile)’ differs between new and previous==> NOT changing ‘onefile’"

Any advice on how to move forward with this would help. I am using the maptools package to try to make these maps.

Cheers, Israel

I Del Toro
  • 913
  • 4
  • 15
  • 36
  • 1
    There's a _lot_ going wrong here. Let's start with how to write a basic `for` loop. `i in 1:length(species)` means that each time through the loop, `i` with take the values 1, 2, 3,...up to the length of `species`. When you refer to `species` inside the loop, you're getting the entire vector, not a single value. Perhaps you meant `species[i]` in each case? – joran Jan 31 '13 at 02:41
  • Thank you. Even if I change the species inside the loop to species[i], I still get the same error message. Not sure what it all means . – I Del Toro Jan 31 '13 at 02:47
  • Yes, that's because this is only one of many, many things you need to fix. Next up: your `pdf()` call seems to be missing the use of `paste`. If you read `?pdf` you'll see that passing `species` like that is just going to be ignored completely. What I think you _meant_ is for the species name to be joined with `.pdf`, but to do that you need `pdf(paste0(species[i],".pdf"),width=5,height=4)`. – joran Jan 31 '13 at 02:50
  • Next we have the `points()` line. This will probably take two comments. Before the `for` loop you set `species <- levels(raw$SpCode)`, which means that `species` will be a character vector of species names. But in this line, `species$latitude` and species$longitude` suggest that you _think_ `species` should be a data frame with columns called latitude and longitude that contain the x,y coords you'd like to plot. Maybe latitude and longitude are in raw instead...? Only you know that for sure. – joran Jan 31 '13 at 02:58
  • No, you'll want something like `points(raw$longitude[raw$SpCode == species[i]],...)`. – joran Jan 31 '13 at 03:03
  • Thanks joran. You are correct, the xy coordinates are in the raw file not the species vector. How can I call them up in the points() so they match up with the species vector? – I Del Toro Jan 31 '13 at 03:05
  • Thanks joran. You are correct, The code you suggested fixed me right up. Cheers! – I Del Toro Jan 31 '13 at 03:15
  • @Del Toro, perhaps you could paste your final code that worked as an answer and mark it as answered so that the question is marked answered? – Arun Jan 31 '13 at 09:26

1 Answers1

0

loop for making species map 1.2 save as a pdf

Thanks Joran for helping me figure this out! :)

species <- levels(raw$SpCode)
for(i in 1:length(species))  
            {
            #open the file for writing
             pdf(paste0(species[i],".pdf"),width=5,height=4)
             plot (wrld_simpl, xlim=c(-100,-55), ylim=c(23,63), axes=TRUE, col='light grey')
             box() #adds box around map
             title(main=species[i]) #adds main title to map which should be the species name associated with the data
             points(raw$longitude[raw$SpCode == species[i]],raw$latitude[raw$SpCode == species[i]], col='black', pch=21, bg="red", cex=0.85)
             dev.off()
             }
Community
  • 1
  • 1
I Del Toro
  • 913
  • 4
  • 15
  • 36