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

David L Carlson dcarlson at tamu.edu
Sat Sep 15 20:40:08 CEST 2012


Typo correction - brackets around pch[] and bg[]:

points(map$longitude, map$latitude, pch=pch[as.numeric(map$number)],
    col="black", bg=bg[as.numeric(map$number)], cex=1.5)

-------
David


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of David L Carlson
> Sent: Saturday, September 15, 2012 1:33 PM
> To: 'Neele'; r-help at r-project.org
> Subject: Re: [R] plotting points on a map, assigning vectors to values
> from dataset
> 
> You didn't include the data so we can't really test anything, but
> you can simplify things quite a bit by replacing the pch= and bg=
> lines with
> 
> pch <- c(24, 23, 22, 21)
> bg <- c("grey24", "black", "grey77", "white")
> 
> map$number is a factor which means your categories default to
> alphabetical order so the first category (factor code 1) is
> fourfive and the second category (factor code 2) is sixseven.
> The above lines take this into account.
> 
> Then replace the whole for loop (you really need to learn about
> vectorization in R) with a single function call:
> 
> points(map$longitude, map$latitude, pch=pch(as.numeric(map$number)),
>    col="black", bg=bg(as.numeric(map$number)), cex=1.5)
> 
> ----------------------------------------------
> David L Carlson
> Associate Professor of Anthropology
> Texas A&M University
> College Station, TX 77843-4352
> 
> 
> 
> 
> 
> 
> > -----Original Message-----
> > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> > project.org] On Behalf Of Neele
> > Sent: Friday, September 14, 2012 12:41 PM
> > To: r-help at r-project.org
> > Subject: [R] plotting points on a map, assigning vectors to values
> from
> > dataset
> >
> > 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","sixseve
> > n"="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.
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-
> > guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list