ggmap删除了包含缺失值的行(geom_point)

时间:2019-05-14 03:43:28

标签: r ggplot2 geospatial ggmap

我正在尝试在ggmap图上绘制纬度和经度。我可以从get_map正确获取地图。当我使用geom_point时,它会丢弃我的所有行,并且什么也不做。我最终不得不输入纬度和经度作为字符,因为它使用col_double()删除数据

我尝试将坐标的数据类型更改为as.numeric(latitude),然后更改为as.numeric(as.character(latitude))

导入数据

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) 

我希望它能标出位置

1 个答案:

答案 0 :(得分:0)

您在aes语句中混合了纬度和经度。纬度为y,经度为x

除此之外,我再也找不到错误。

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