learning
learning

Reputation:

Plotting points on a map

I am trying to plot coordinate points on a map, but I get the plot.new error. Could you please help?

library(maptools)
library(ggmap)
library(mapproj)    
table <- read.table("table.txt", header=TRUE, sep=",")
map <- get_map(location = 'France', zoom = 6, maptype = c("toner"))
points(table$LONG, table$LAT, pch=21, bg=color, cex=0.7, lwd=.4)
ggmap(map)

Here is an idea of what the table looks like:

CITY,LAT,LONG
Paris,48.856667,2.351944
Lyon,45.766944,4.834167
Bordeaux,44.838611,0.578334

Upvotes: 0

Views: 1325

Answers (2)

Penguin_Knight
Penguin_Knight

Reputation: 1299

Try geom_point:

library(maptools)
library(ggmap)
library(mapproj)

city <- c("Paris", "Lyon", "Bordeaux")
my.lat <- c(48.856667, 45.766944, 44.838611)
my.long <- c(2.351944, 4.834167, 0.578334)

points <- data.frame(lon=my.long, lat=my.lat)

map <- get_map(location = c(left = -5, bottom = 42, right=9, top = 51 ), source = 'stamen', maptype = 'toner')
france <- ggmap(map, extent = 'normal')
france + geom_point(data=points, col="red")

enter image description here

Try the command ?ggmap for a list of great examples. I think the manual has done a good job, because before I read your question, I didn't even know of any of these functions. Thanks! I've learned something new.

Upvotes: 3

Spacedman
Spacedman

Reputation: 94192

Learn to walk before you try and run.

The points function adds points to an existing graphic. You haven't got an existing graphic yet (unless you've already done something you've not showed us).

Hence if you do points before starting a plot, you'll get an error. eg:

points(1:10,1:10) # plot.new error
plot(1:10,1:10) # no error, starts a new plot
points(10:1,1:10) # adds extra points, no error.

All your stuff with ggplot is irrelevant. Also, this is not about statistics, so you should have posted to StackOverflow. I've flagged this and it might get migrated...

Upvotes: 2

Related Questions