[R] plotting points on a map, assigning vectors to values from dataset

Neele Neekropolis at gmx.net
Fri Sep 14 19:41:20 CEST 2012


Dear everyone, as a newbie I fear I have made an easy mistake and am just too
blind to see it. Can anyone help me seeing the (possibly) obvious?
I  have a command that creates a map of South America. In a separate
txt-file I stored data about latitude and longitude of languages, and in a
third column values that differ from language to language. The command
should read that file and assign the values to pch vectors (with bg as well,
because they are 21-24) and plot them on the map.
Now, the map is properly displayed, and the location of the pch symbols is
correct as well. But the actual symbols (and bg colors) do not match the
original data in the txt-file. (e.g. a white circle should display languages
with value "zeroone", and a black diamond those with "sixseven", but there
are white diamonds for "zeroone". That is easy to change if I only had two
values, but I have four, and whatever I change it never matches the actual
data).
I want to automatically match my values (e.g. zeroone, twothree etc.) with
shapes from pch and background colors. That works fine in the legend, but
not for the actual map. I guess there is something missing so that the
command does not assign the correct vectors to the correlation rows of the
matrix.
I specified the pch and bg vectors and then tried (
pch=pch[[map$number[i]]], bg=bg[[map$number[i]]]), because the header of the
column is "number", to make it read the correct column, but I guess
somewhere the link is missing. 
The legend is displayed correctly, the retrieval path for the txt.file is
correct, and the .pdf is also created. 
 I had problems because the lat/long values were read as factors and not as
numericals at first; I changed that with gsub and it works. Do I perhaps
have to change the values for the "number" column as well? (I put str(map)
at the end below, it looks all right so far to me...)

I hope you understand my problem, and like I said, it is probably something
very easy and clear. I found lots of info about point, pch, bg etc., but not
of how to automatically assign values to points using a data file. 

I am grateful for any comment! 
Best wishes,
Neele


library(maps)
library(mapdata)
map = read.delim("C:/Users/Neele/Desktop/number_of_modals.txt", header=TRUE)

map$longitude=gsub(",",".",map$longitude)
map$longitude=as.numeric(as.character(map$longitude))

map$latitude=gsub(",",".",map$latitude)
map$latitude=as.numeric(as.character(map$latitude))

xlim = c(-85,-35)
ylim = c(-55,15)

pch = list("zeroone"=21, "twothree"=22,"fourfive"=24,"sixseven"=23)

bg =
list("zeroone"="white","twothree"="grey77","fourfive"="grey24","sixseven"="black")
          
          pdf('C:/Users/Neele/Desktop/number_of_modals.pdf', height=8,
width=6)
          
          map("worldHires",xlim=xlim, ylim=ylim,cex.lab=0.5)
          
          map.axes()
          
          for (i in 1:nrow(map)) {
          points(map$longitude[i],map$latitude[i],   *until here everything
as it should be*
          pch=pch[[map$number[i]]], *but here there seems to be random
assigning of vector to value*
          bg=bg[[map$number[i]]], 
          col="black", 
          cex = 1.5)
          }
          title(main="Number of marked Modals")
          legend("bottomright", 
          legend=c("0-1","2-3","4-5","6-7"), 
          title="Legend", 
          inset=0.05,
          pch=c(21,22,24,23),
          pt.bg=c("white","grey77","grey24","black")
          )
          
          dev.off()

> str(map)
'data.frame':	63 obs. of  6 variables:
 $ latitude : num  -5 -9 1.5 -17 -13.1 ...
 $ longitude: num  -78 -67 -78.2 -69 -64.2 ...
 $ family   : Factor w/ 26 levels "Araucanian","Arawakan",..: 13 2 5 4 2 14
22 9 24 23 ...
 $ language : Factor w/ 63 levels "Aguaruna","Apurina",..: 1 2 3 4 5 6 7 8 9
10 ...
 $ number   : Factor w/ 4 levels "fourfive","sixseven",..: 3 1 1 3 1 3 1 4 3
1 ...     
 $ X        : logi  NA NA NA NA NA NA ...
> 


 



--
View this message in context: http://r.789695.n4.nabble.com/plotting-points-on-a-map-assigning-vectors-to-values-from-dataset-tp4643182.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list