0

I'm simply plotting lat/longs from a file onto a map in R. I'm having some issues as the points I plot are displayed in roughly the correct area - but not accurate enough for me. I don't know if this is my code, the google map, or the resultion of the lat/longs? I think I've eliminated the latter (see below).

Code:

library (ggmap)
library (gdata)

whitelee_boundary <- c(left = -4.6, bottom = 55.5, right = -3.8, top = 55.8)
map2 <- get_stamenmap(whitelee_boundary, zoom = 12, maptype = "toner-lite")
ggmap(map2)

df <- read.table("WhiteleeLatLong.txt", header = FALSE)
lat=df[1]
lon=df[2]
lati=as.numeric(unlist(lat))
long= as.numeric(unlist(lon))

points(lati, long, col = "blue", cex = 1.0)

which results in blue circles plotted in a very tight(incorrect) geographical range

blue circles plotted in a very tight(incorrect) geographical range

I then attempted to error check the points by manually displaying:

points(55.7, -4.4, col = "blue", cex = 1.0)

This results in no point being visibly added (making me think that this is my code or google map accuracy).

I originally thought this was a precision issue in read.table or numeric conversion, and have already consulted:

How to stop read.table from rounding numbers with different degrees of precision in R?

as.numeric() removes decimal places in R, how to change?

However it seems that the rounding is only aesthetic, and the numbers are stored with the requisite precision.

Apologies in advance - its been a very long time since my last coding task!

EDIT: When I command

points(-4.4, 55.7, col = "blue", cex = 1.0)

THe output is (ignore large circles):

Plot appears on map but not in correct location

  • Can you share a few of your long and lat points as they are? – Phil Jan 08 '18 at 16:29
  • sure: 55.68918453 -4.356793868 – lemsip_max Jan 08 '18 at 16:37
  • Your points call is wrong: switch the x and y (55.7 is a lat or y; -4.4 is a long or x), i.e. `points(-4.4, 55.7, col = "blue", cex = 1.0)` – Phil Jan 08 '18 at 16:40
  • It's not a question of accuracy, then! 2 or 3 decimal places is more than enough for this scale map. TBH, 8 decimal places is probably spurious accuracy, anyway. What are the minimum and maximum of your longitude and latitude coordinates? – Phil Jan 08 '18 at 16:43
  • Thx Phil- Max/ mins : 55.71402019 55.6552228 and -4.362110752 -4.183033069 – lemsip_max Jan 08 '18 at 17:01
  • I tried the x/y switch - point is being plotted but not in the right place. See edit to original question for detail. – lemsip_max Jan 08 '18 at 17:03
  • I don't know how you're using `points()` to add to a ggplot. When I try that, I get an error: `Error in plot.xy(xy.coords(x, y), type = type, ...) : plot.new has not been called yet`. But if you name you say `my_map = ggmap(map2)` and then add `my_map + geom_point(y = 55.7, x = -4.4, col = "blue", size = 1.0)`, the dot appears in just the right place. So overall **I can't reproduce what you are plotting, and it seems like a typo to try to use base plotting command with ggplot, so I'm voting to close.** – Gregor Thomas Jan 08 '18 at 17:27
  • OK - I appreciate the feedback – lemsip_max Jan 08 '18 at 17:33
  • I think to help further you really need to provide a few points from `WhiteleeLatLong.txt`, i.e. provide a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Phil Jan 09 '18 at 10:13

0 Answers0