我试图在 map (shapefile)中绘制点,但我做不到.我用lat和long st_transform('+init=epsg:4326')转换了shapefile,因为我的dat包含lat和long信息.

然后,我绘制但不起作用.

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))

Data (long, 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之前删除它,绘图调用就会正常工作:

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()

nxparroquias.shp

顺便说一句,您可能需要考虑切换到更具互动性(如mapview)的方式来对空间数据进行探索性分析:

library(mapview)

m_map = mapview(
  map
  , legend = FALSE
)


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

m_map + 
  m_dat

mapview

R相关问答推荐

如何使用geom_sf在边界显示两种 colored颜色 ?

次级y轴R gggplot2

手动打印线型gplot

更改默认系列1以更改名称

用预测NLS处理R中生物学假设之上的误差传播

制作等距离的线串副本

我如何才能找到FAMILY=POISSON(LINK=&Q;LOG&Q;)中的模型预测指定值的日期?

来自程序包AFEX和amp;的类/函数和NICE_TABLE&冲突

如何使用同比折线图中的个别日

'使用`purrr::pwalk`从嵌套的嵌套框架中的列表列保存ggplots时出现未使用的参数错误

我如何使用循环来编写冗余的Rmarkdown脚本?

数值型数据与字符混合时如何进行绑定

R中治疗序列的相对时间指数

使用ggplot2绘制具有边缘分布的坡度图

每行不同列上的行求和

带有Bootswatch Cerulean主题的shiny 仪表板中的浏览&按钮可见性问题

如何修复geom_rect中的层错误?

如何修改Rust中的R字符串并将其赋给新的R变量,并使用extendr保留原始R字符串

使用另一列中的增长率外推R(使用dplyr)

如何在用`{{ }}`创建的变量上使用整洁 Select ?