0
votes

I am trying to map latitude and longitude on a ggmap plot. I am able to get the map correctly from get_map. When I am using the geom_point, it is dropping all my rows and plotting nothing. I ended up having to import the latitude and longitude as character as it was dropping data with col_double()

I have tried to change the data type of my coordinates, first to as.numeric(latitude) and then as as.numeric(as.character(latitude))

Import the data

df2 <- read_csv(path2, col_types =cols(
  Address = col_character(),
  City = col_character(),
  State = col_character(),
  `Zip Code` = col_character(),
  Latitude = col_character()
  Longitude = col_character()
))

df2 <-
  tibble(
    Latitude = c(32.8, 40.6),
    Longitude = c(-117, -122)
  )

map <- get_map(location = "california", zoom=3)
ggmap(map) + geom_point(data = df2[df2$State=='California',], aes(x = as.numeric(Latitude), y = as.numeric(Longitude)), color="red", size = 5, shape = 21) 

I would expect it to plot locations

1

1 Answers

1
votes

You have mixed up latitude and longitude in your aes statement. Latitude is y and longitude is x.

Apart from that I couldn't find any more errors.

library(ggmap)

> bb
         min       max
x -124.48200 -114.1308
y   32.52952   42.0095


map <- get_map(location = bb)

coords <- data.frame(lat = c(32.8, 40.6),
                     long = c(-117, -122))


ggmap(map) +
  geom_point(data = coords, aes(x = long, y = lat), color="red", size = 5, shape = 21)

enter image description here