4
votes

I have a map of the USA and a list of long, lat that I want to plot. Once I get this working, I also want to switch over to the "world" map. The map is generated, but no points appear on the map. The first line of the TSV file contains this header:

LONG{tab}LAT
R appears to be reading in the 'traffic' table OK. What am I doing wrong?
library("maps")

traffic = read.table("C:/temp/traffic_10.40.tsv", header=T, sep="\t")
png(filename="C:/temp/usa.png", width=850, height=600, bg="white")
map('state', plot = TRUE, fill = FALSE, col = palette())
title("Destinations")
points(x=traffic$LONG,y=traffic$LAT,col='red',cex=0.75)
dev.off()

EDIT

> dput(traffic)
structure(list(LONG = c(47.6218, 32.7942, 34.1121, 40.0068, 47.6218,
33.9553, 33.7629, 40.0068, 39.05, 38.1075, 33.7629, 32.769, 37.3857,
29.4576, 34.1674, 38.8147, 32.7942, 31.1429, 40.3254, 30.3059,
38.2248, 47.6218, 33.9553, 38.1075, 27.1943, 29.4576, 30.5175,
38.5668, 42.6657, 40.2982, 32.7539, 40.6698, 47.6742, 32.7942,
47.6218, 35.8219), LAT = c(-122.35, -96.7653, -118.411, -75.1347,
-122.35, -83.3937, -84.4226, -75.1347, -77.4833, -122.264, -84.4226,
-96.5998, -122.026, -98.5054, -84.8014, -77.0647, -96.7653, -81.471,
-78.9195, -97.7505, -85.7412, -122.35, -83.3937, -122.264, -80.2432,
-98.5054, -97.6721, -121.467, -73.799, -111.698, -97.3363, -73.9438,
-122.115, -96.7653, -122.35, -78.6588)), .Names = c("LONG", "LAT"
), class = "data.frame", row.names = c(NA, -36L))

Also, I am a R newbie and have tried finding this on google with limited success because I am not sure what to search for exactly.

3
Hard to say without access to your data. Any way to post the .tsv or png online?Jeff Allen
or ... post the data online? the easiest way is to copy and paste the results of dput(traffic); or if it a big data set, try dput(traffic[1:10,]David LeBauer
Your map of the USA appears to be a PNG file. This has no geographical coordinates associated to it, so there's no way of knowing where a given lat-long point is. It might even be in a funny projection, or Alaska could be tucked down off the coast of California for compactness, and Hawaii in the Gulf. You need a proper georeferenced raster file, or knowledge of the projection and coordinates of the PNG.Spacedman
It might be worth reposting/moving the post over to gis.stackexchange.com There are a few R and mapping over questions there.djq
@jftuga, thanks for asking this question. I am new to spatial statistics and was trying for hours to generate a map with plotted points yesterday. Your example is exactly what I was looking for.Vivek Subramanian

3 Answers

8
votes

The problem is in your data set rather than your later code.

The first point has one co-ordinate 47.6218 and the other -122.35. Latitudes cannot be outside the range [-90,90] degrees so the longitude must be -122.35 and latitude 47.6218, the opposite of your data set. This is slightly north of the Seattle Space needle

x (horizontal) is traditionally longitude or easting; y (vertical) is traditionally latitude or northing

1
votes

One option would be to use googleVis for this. You could plot out your points Google-Maps style.

1
votes

Fixed. I swapped $LAT and $LONG. Now it works perfectly.

points(x=traffic$LAT,y=traffic$LONG,col='red',cex=0.75)