0

I wanted to plot coordinates from a dataset onto a map, I used the code I found from Here. My code looked like this:

# creating a sample data.frame with your lat/lon points
lon <- c(known_long_lat$takeoff.longitude)
lat <- c(known_long_lat$takeoff.latitude)
df <- as.data.frame(cbind(lon,lat))

# getting the map
mapgilbert <- get_map(location = c(lon = mean(df$lon), lat = mean(df$lat)), zoom = 4, maptype = "satellite", scale = 2)

# plotting the map with some points on it
ggmap(mapgilbert) +
geom_point(data = df, aes(x = lon, y = lat, fill = "red", alpha = 0.8), 
size = 5, shape = 21) +
guides(fill=FALSE, alpha=FALSE, size=FALSE)`

When I run this code I get this error:

Error in if (lon < -180 || lon > 180) { : missing value where TRUE/FALSE needed In addition: Warning messages: 1: In mean.default(df$lon) : argument is not numeric or logical: returning NA 2: In mean.default(df$lat) : argument is not numeric or logical: returning NA

I thought maybe it was because I had non-numeric data in the latitude and longitude columns but there doesn't seem to be.

Niall
  • 518
  • 1
  • 6
  • 23
  • can you share some data, it might be that some data falls outside the range (-180/180) – timfaber Apr 05 '17 at 14:02
  • Check the output of `mean(df$lon)` and `mean(df$lat)`, they seem to be NA. You probably need `mean(df$lon, na.rm = T)` – yeedle Apr 05 '17 at 14:03
  • It turns out that I did have non-numeric values in the longitude column as I had only checked the latitude column. But now when I run it I get Warning message: In cbind(lon, lat) : number of rows of result is not a multiple of vector length (arg 1) – Niall Apr 05 '17 at 14:16
  • if you remove NAs from lon, your lat and lon vectors are not the same length when binding them. Easiest is to use `mean(lon,na.rm=T)` and `mean(lat,na.rm=T)` as input to `get_map` – timfaber Apr 05 '17 at 14:38
  • I manually made a smaller version of my dataset and when I could make sure there was nothing wrong I no longer get that error. Unfortunately I'm having a different problem where it's removing rows that it says are containing missing values although all the longitudes and latitudes are definitely there. – Niall Apr 08 '17 at 12:40
  • Fixed that last error I had, I just had my zoom set wrong – Niall Apr 08 '17 at 12:54

0 Answers0