[R-sig-Geo] plotting geographic coordinates on a map

Ashton Shortridge ashton at msu.edu
Thu Jul 14 18:07:35 CEST 2011


On 2011-07-14, atlasrrg, wrote:
> Hello,
> 
> I am pretty sure that I am making a novice mistake here as I've only just
> begun exploring spatial data in R. I have a data.csv file that contains
> geographic coordinates as follows:
> 
> SEQUENCE,longitude,latitude,altitude
> AF196835_Pc_40.77_73.96_1999.71,-73.96,40.77,18
> AF202541_Hs_42.76_75.82_1999.66,-75.82,42.76,456
> AF260967_Ec_40.77_73.96_1999.50,-73.96,40.77,18
> AF404753_Cb_39.43_76.78_2000.50,-76.78,39.43,154
> AF404754_Cp_40.95_74.07_2000.50,-74.07,40.95,18
> ...
> 
> I simply would like to plot these on a map of the US. I've tried all sorts
> of approaches, including
> 
> > map('usa', project='albers', par=c(39,34))
> > points(x=data$LONGITUDE, y=data$LATITUTUDE, col='red')
> 
> [as well as several other maps in the maps package]
> 
> and
> 
> > all_states <-map_data("state")
> > p <- ggplot()
> > p <-p + geom_polygon( data=all_states, aes(x=long, y=lat, group = group),
> > colour='white', fill = 'grey10')
> > p
> > mydata <-read.csv("geo.csv", header=TRUE, row.names=1, sep=",")
> > p <- ggplot()
> > p <- p+ geom_point( data=mydata)
> 
>  and
> 
> > library(sp)
> > coordinates(WNV) <- c("latitude", "longitude")
> > map('world')
> > plot(WNV$latitude, WNV$longitude)
> 
> I was able to plot an example file coordinates on the world map in the maps
> package, but not on the US map. If someone could please point me in the
> right direction I would greatly appreciate your help!!
> 
> Thank you
> 
> Rebecca
> 

Hi Rebecca,

R is case-sensitive, so assuming you read in that csv file, you would want to 
refer to the columns with lower-case letters, e.g.:
data <- read.csv('myfilename')
names(data)  # make sure they are what you think they are.
data[1:10,]    # look at the first 10 rows of the data, too!
map('usa', project='albers', par=c(39,34))
points(x=data$longitude, y=data$latitude, col='red')

Hope this helps,

Ashton
-----
Ashton Shortridge
Associate Professor			ashton at msu.edu
Dept of Geography			http://www.msu.edu/~ashton
235 Geography Building		ph (517) 432-3561
Michigan State University		fx (517) 432-1671



More information about the R-sig-Geo mailing list