在地图中绘制点(经度和纬度)ggplot2

Plotting points in map (long and lat) ggplot2

我正在尝试在地图上绘制点 (shapefile),但我做不到。我用经纬度 st_transform('+init=epsg:4326') 转换了 shapefile,因为我的 dat 有经纬度信息。

然后我绘图但不起作用。

map = st_read('nxprovincias.shp') %>% 
  sf::st_transform('+init=epsg:4326')

# I've tried using `geom_sf`

dat1 = dat %>%
  st_as_sf(coords = c("long", "lat"), crs=4326)

ggplot() +
  geom_sf(data = map) +
  geom_sf(data = dat1, aes(geometry = geometry))

# and `geom_point`

ggplot() +
  geom_sf(data = map) +
  geom_point(data = dat, aes(x = long, y = lat))

数据(经度、纬度、x)

dat = structure(list(lat = structure(c(-2.87660479545593, -2.87720417976379, 
-2.87735748291016, -2.87753105163574, -2.87765717506409, -2.87785005569458, 
-2.87821888923645, -2.87918782234192, -2.87929964065552, -2.87667083740234, 
-2.87697672843933, -2.87707996368408, -2.87767362594604, -2.87771010398865, 
-2.87813591957092, -2.8781750202179, -2.8784019947052, -2.87957549095154, 
-2.87958645820618, -2.87968635559082, -2.87970232963562, -2.87977194786072, 
-2.87977933883667, -2.87978482246399, -2.87985110282898, -2.87985396385193, 
-2.87991166114807, -2.87996673583984, -2.87998247146606, -2.88041758537292, 
-2.9928183555603, -99, -2.87677383422852, -2.87691879272461, 
-2.87718558311462, -2.87721037864685, -2.87743043899536, -2.87768173217773, 
-2.87944602966309, -2.87797331809998, -2.87819075584412, -2.87830853462219, 
-2.87849140167236, -2.8785994052887, -2.87917923927307, -2.87923359870911, 
-2.87934041023254, -2.87948775291443, -2.88050103187561, -2.88078212738037, 
-2.88109421730042, -2.88113117218018, -2.88172602653503, -2.88214111328125, 
-2.88219523429871, -2.87862133979797, -2.88026261329651, -2.88060832023621, 
-2.88061451911926, -2.88077187538147, -2.88077616691589, -2.88100337982178, 
-2.88157868385315, -2.8817310333252, -2.88299989700317, -2.89299464225769, 
-2.88181924819946, -2.88214421272278, -2.88239336013794, -2.88244104385376, 
-2.88291192054749, -2.88306641578674, -2.87702965736389, -2.87748551368713, 
-2.87786865234375, -2.87825655937195, -2.87838006019592, -2.88284087181091, 
-2.87875247001648, -2.88032579421997, -2.88060545921326, -2.87759780883789, 
-2.87762522697449, -2.8776683807373, -2.87819457054138, -2.87915062904358, 
-2.87936305999756, -2.87957811355591, -2.87959146499634, -2.87961769104004, 
-2.88021159172058, -2.88076829910278, -2.88081574440002, -2.88141989707947, 
-2.87116622924805, -2.87180852890015, -2.87283968925476, -2.87302923202515, 
-2.87308740615845, -2.87557435035706), format.stata = "%9.0g"), 
    long = structure(c(-79.0676956176758, -79.0701141357422, 
    -79.0691986083984, -79.067756652832, -79.0691986083984, -79.0691986083984, 
    -79.0691528320312, -79.0684051513672, -79.0679779052734, 
    -79.0663223266602, -79.0669784545898, -79.0658645629883, 
    -79.0663909912109, -79.0657043457031, -79.0673751831055, 
    -79.0672378540039, -79.0664520263672, -79.0665512084961, 
    -79.0662689208984, -79.0657424926758, -79.0663528442383, 
    -79.0650329589844, -79.0666046142578, -79.0664138793945, 
    -79.0665588378906, -79.0667877197266, -79.0649337768555, 
    -79.0649490356445, -79.0650482177734, -79.064826965332, -79.0410537719727, 
    -99, -79.0623397827148, -79.0616836547852, -79.0619812011719, 
    -79.061897277832, -79.0632171630859, -79.0630722045898, -79.061653137207, 
    -79.0590896606445, -79.0603866577148, -79.0595092773438, 
    -79.0588912963867, -79.0578765869141, -79.0596008300781, 
    -79.0606155395508, -79.0592498779297, -79.0592041015625, 
    -79.0583572387695, -79.0598678588867, -79.0614395141602, 
    -79.0602798461914, -79.0587768554688, -79.0586318969727, 
    -79.0586547851562, -79.0604934692383, -79.0666580200195, 
    -79.0646667480469, -79.0649719238281, -79.0640106201172, 
    -79.0656890869141, -79.0631713867188, -79.059700012207, -79.0645904541016, 
    -79.0590209960938, -78.9783630371094, -79.0576248168945, 
    -79.0585327148438, -79.0580749511719, -79.0582504272461, 
    -79.0576858520508, -79.0575942993164, -79.0545349121094, 
    -79.0535278320312, -79.0556869506836, -79.0555191040039, 
    -79.0541076660156, -79.0554046630859, -79.0519485473633, 
    -79.052360534668, -79.052848815918, -79.0486145019531, -79.0485687255859, 
    -79.0481719970703, -79.0492935180664, -79.0472640991211, 
    -79.0477523803711, -79.0483016967773, -79.04833984375, -79.0483245849609, 
    -79.047981262207, -79.0500640869141, -79.0481643676758, -79.0477676391602, 
    -79.0512161254883, -79.050537109375, -79.0501861572266, -79.0501327514648, 
    -79.0500335693359, -79.0494155883789), format.stata = "%9.0g"), 
    x = structure(c(0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 
    0, 0, 1, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 
    0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 1, 
    0, 1, 0, 1, 0, 0, 0, 0, 1, 2, 0, 0, 0, 1, 0, 1, 0, 1, 0, 
    2, 0, 0, 0, 2, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 1, 0, 0, 0), format.stata = "%8.0g")), row.names = c(NA, 
-100L), class = c("tbl_df", "tbl", "data.frame"))

你能详细说明一下到底是什么不起作用吗?您的数据集中有一处坐标无效:

subset(
  dat
  , lat < -90
)
# # A tibble: 1 × 3
#   lat  long     x
# <dbl> <dbl> <dbl>
#   -99   -99     2

在转换为 sf 之前删除它,您的 plot 调用工作正常:

dat1 = dat |> 
  subset(
    lat >= -90
  ) |> 
  st_as_sf(
    coords = c("long", "lat")
    , crs = 4326
  )

ggplot() +
  geom_sf(data = map) +
  geom_sf(data = dat1, aes(geometry = geometry), color = "red") + 
  theme_minimal()

顺便说一句,您可能想考虑切换到更具交互性的东西(如 mapview)以进行空间数据的探索性分析:

library(mapview)

m_map = mapview(
  map
  , legend = FALSE
)


m_dat = mapview(
    dat1
    , layer.name = "x"
  )

m_map + 
  m_dat