我正在尝试在地图上添加坐标点。我的代码可以与 rgdal 包一起使用,但我不能再使用它了。现在我正在尝试使用 sf 包来调整旧代码。
我正在通过 geobr 包加载我的地图:
library(sf)
library(geobr)
brasil <- read_state(year = 2020, showProgress = FALSE)
坐标基如下:
structure(list(geometry = structure(list(structure(c(-68.75375,
-10.99904), class = c("XY", "POINT", "sfg")), structure(c(-72.72181,
-7.6002), class = c("XY", "POINT", "sfg")), structure(c(-70.34421,
-8.16137), class = c("XY", "POINT", "sfg")), structure(c(-67.19084,
-10.31946), class = c("XY", "POINT", "sfg")), structure(c(-67.8156,
-9.96015), class = c("XY", "POINT", "sfg")), structure(c(-67.81134,
-9.97451), class = c("XY", "POINT", "sfg")), structure(c(-67.74061,
-10.15484), class = c("XY", "POINT", "sfg")), structure(c(-68.49207,
-10.65385), class = c("XY", "POINT", "sfg")), structure(c(-36.65936,
-9.73472), class = c("XY", "POINT", "sfg")), structure(c(-43.34302,
-22.85825), class = c("XY", "POINT", "sfg")), structure(c(-67.81134,
-9.97451), class = c("XY", "POINT", "sfg")), structure(c(-35.673,
-9.58896), class = c("XY", "POINT", "sfg")), structure(c(-35.75334,
-9.58608), class = c("XY", "POINT", "sfg")), structure(c(-35.72666,
-9.66578), class = c("XY", "POINT", "sfg")), structure(c(-36.61828,
-9.42297), class = c("XY", "POINT", "sfg")), structure(c(-36.36252,
-9.90909), class = c("XY", "POINT", "sfg")), structure(c(-50.79231,
2.04788), class = c("XY", "POINT", "sfg")), structure(c(-52.50741,
-0.82827), class = c("XY", "POINT", "sfg")), structure(c(-51.05806,
0.01289), class = c("XY", "POINT", "sfg")), structure(c(-51.06114,
0.01443), class = c("XY", "POINT", "sfg")), structure(c(-51.05496,
0.03629), class = c("XY", "POINT", "sfg")), structure(c(-51.28724,
-0.12125), class = c("XY", "POINT", "sfg")), structure(c(-51.83473,
3.84354), class = c("XY", "POINT", "sfg")), structure(c(-51.1667,
-0.05409), class = c("XY", "POINT", "sfg")), structure(c(-58.44538,
-3.13875), class = c("XY", "POINT", "sfg")), structure(c(-60.62821,
-3.2936), class = c("XY", "POINT", "sfg")), structure(c(-60.02361,
-3.13371), class = c("XY", "POINT", "sfg")), structure(c(-60.04465,
-3.06856), class = c("XY", "POINT", "sfg")), structure(c(-60.04587,
-3.06695), class = c("XY", "POINT", "sfg")), structure(c(-63.15203,
-4.09908), class = c("XY", "POINT", "sfg")), structure(c(-60.02243,
-3.13368), class = c("XY", "POINT", "sfg")), structure(c(-59.9205,
-3.04288), class = c("XY", "POINT", "sfg")), structure(c(-60.07575,
-2.66348), class = c("XY", "POINT", "sfg")), structure(c(-59.99029,
-3.0361), class = c("XY", "POINT", "sfg")), structure(c(-57.71528,
-3.38232), class = c("XY", "POINT", "sfg")), structure(c(-56.75037,
-2.64711), class = c("XY", "POINT", "sfg")), structure(c(-60.02245,
-2.05228), class = c("XY", "POINT", "sfg")), structure(c(-64.71758,
-3.35678), class = c("XY", "POINT", "sfg")), structure(c(-38.40094,
-12.14792), class = c("XY", "POINT", "sfg")), structure(c(-44.99695,
-12.11793), class = c("XY", "POINT", "sfg")), structure(c(-38.95337,
-12.28382), class = c("XY", "POINT", "sfg")), structure(c(-38.95339,
-12.28289), class = c("XY", "POINT", "sfg")), structure(c(-39.03736,
-14.79573), class = c("XY", "POINT", "sfg")), structure(c(-38.68111,
-12.88374), class = c("XY", "POINT", "sfg")), structure(c(-40.51825,
-11.17427), class = c("XY", "POINT", "sfg")), structure(c(-40.08238,
-13.83787), class = c("XY", "POINT", "sfg")), structure(c(-38.50352,
-12.97553), class = c("XY", "POINT", "sfg")), structure(c(-38.21926,
-9.4469), class = c("XY", "POINT", "sfg")), structure(c(-39.09503,
-16.41395), class = c("XY", "POINT", "sfg")), structure(c(-38.45646,
-12.97978), class = c("XY", "POINT", "sfg")), structure(c(-38.50448,
-12.97526), class = c("XY", "POINT", "sfg")), structure(c(-38.45588,
-12.97658), class = c("XY", "POINT", "sfg")), structure(c(-38.50442,
-12.96904), class = c("XY", "POINT", "sfg")), structure(c(-38.45543,
-12.97821), class = c("XY", "POINT", "sfg")), structure(c(-38.50918,
-12.97297), class = c("XY", "POINT", "sfg")), structure(c(-38.50998,
-12.96984), class = c("XY", "POINT", "sfg")), structure(c(-38.50165,
-12.95363), class = c("XY", "POINT", "sfg")), structure(c(-38.38748,
-12.95468), class = c("XY", "POINT", "sfg")), structure(c(-38.51373,
-12.97563), class = c("XY", "POINT", "sfg")), structure(c(-38.50815,
-12.97109), class = c("XY", "POINT", "sfg")), structure(c(-39.23695,
-12.98125), class = c("XY", "POINT", "sfg")), structure(c(-40.85474,
-14.84014), class = c("XY", "POINT", "sfg")), structure(c(-37.79361,
-4.57374), class = c("XY", "POINT", "sfg")), structure(c(-38.61548,
-3.74811), class = c("XY", "POINT", "sfg")), structure(c(-38.62544,
-3.6907), class = c("XY", "POINT", "sfg")), structure(c(-40.67437,
-5.17525), class = c("XY", "POINT", "sfg")), structure(c(-39.40444,
-7.22868), class = c("XY", "POINT", "sfg")), structure(c(-38.56062,
-3.76063), class = c("XY", "POINT", "sfg")), structure(c(-38.5606,
-3.7607), class = c("XY", "POINT", "sfg")), structure(c(-38.50988,
-3.73173), class = c("XY", "POINT", "sfg")), structure(c(-38.48504,
-3.78135), class = c("XY", "POINT", "sfg")), structure(c(-38.51884,
-3.74789), class = c("XY", "POINT", "sfg")), structure(c(-38.48853,
-3.81179), class = c("XY", "POINT", "sfg")), structure(c(-67.81134,
-9.97451), class = c("XY", "POINT", "sfg")), structure(c(-39.40431,
-7.22809), class = c("XY", "POINT", "sfg")), structure(c(-38.5393,
-3.72938), class = c("XY", "POINT", "sfg")), structure(c(-38.52942,
-3.72075), class = c("XY", "POINT", "sfg")), structure(c(-34.8789,
-7.11771), class = c("XY", "POINT", "sfg")), structure(c(-34.89251,
-8.07993), class = c("XY", "POINT", "sfg")), structure(c(-38.56664,
-3.7257), class = c("XY", "POINT", "sfg")), structure(c(-38.48504,
-3.78135), class = c("XY", "POINT", "sfg")), structure(c(-40.89013,
-3.91222), class = c("XY", "POINT", "sfg")), structure(c(-39.29518,
-6.36355), class = c("XY", "POINT", "sfg")), structure(c(-39.29518,
-6.364), class = c("XY", "POINT", "sfg")), structure(c(-39.58159,
-3.50291), class = c("XY", "POINT", "sfg")), structure(c(-39.32376,
-7.22104), class = c("XY", "POINT", "sfg")), structure(c(-39.31595,
-7.19864), class = c("XY", "POINT", "sfg")), structure(c(-38.62244,
-3.78571), class = c("XY", "POINT", "sfg")), structure(c(-39.29124,
-5.20059), class = c("XY", "POINT", "sfg")), structure(c(-38.97016,
-3.61168), class = c("XY", "POINT", "sfg")), structure(c(-40.35442,
-3.67619), class = c("XY", "POINT", "sfg")), structure(c(-40.34867,
-3.69206), class = c("XY", "POINT", "sfg")), structure(c(-40.35047,
-3.68661), class = c("XY", "POINT", "sfg")), structure(c(-38.12839,
-5.24403), class = c("XY", "POINT", "sfg")), structure(c(-48.12242,
-15.82007), class = c("XY", "POINT", "sfg")), structure(c(-48.06386,
-15.81976), class = c("XY", "POINT", "sfg")), structure(c(-47.88667,
-15.79706), class = c("XY", "POINT", "sfg")), structure(c(-47.96205,
-15.80539), class = c("XY", "POINT", "sfg")), structure(c(-47.89843,
-15.80618), class = c("XY", "POINT", "sfg")), structure(c(-47.92221,
-15.82005), class = c("XY", "POINT", "sfg"))), class = c("sfc_POINT",
"sfc"), precision = 0, bbox = structure(c(xmin = -72.72181, ymin = -22.85825,
xmax = -34.8789, ymax = 3.84354), class = "bbox"), crs = structure(list(
input = "SIRGAS 2000", wkt = "GEOGCRS[\"SIRGAS 2000\",\n DATUM[\"Sistema de Referencia Geocentrico para las AmericaS 2000\",\n ELLIPSOID[\"GRS 1980\",6378137,298.257222101,\n LENGTHUNIT[\"metre\",1]]],\n PRIMEM[\"Greenwich\",0,\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n CS[ellipsoidal,2],\n AXIS[\"geodetic latitude (Lat)\",north,\n ORDER[1],\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n AXIS[\"geodetic longitude (Lon)\",east,\n ORDER[2],\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n USAGE[\n SCOPE[\"Horizontal component of 3D system.\"],\n AREA[\"Latin America - Central America and South America - onshore and offshore. Brazil - onshore and offshore.\"],\n BBOX[-59.87,-122.19,32.72,-25.28]],\n ID[\"EPSG\",4674]]"), class = "crs"), n_empty = 0L)), row.names = c(NA,
-100L), sf_column = "geometry", agr = structure(integer(0), levels = c("constant",
"aggregate", "identity"), class = "factor", names = character(0)), class = c("sf",
"tbl_df", "tbl", "data.frame"))
我正在尝试使用以下代码生成带有点的地图:
ggplot(brasil) +
geom_sf(aes(geometry = geom), fill="white") +
labs(title = "Unidades fixas - Brasil") +
geom_sf(data = unidades_lat_long, aes(geometry = geometry), fill = "black") +
theme(legend.position = "none",
plot.title = element_text(hjust = 0.5),
plot.caption = element_text(hjust = 0, face = "italic", lineheight = 1),
axis.title=element_blank(),
axis.text=element_blank(),
axis.ticks=element_blank()) +
theme_nothing(legend = T)
结果是这样的
查看 imgur.com 上的帖子想要的结果
查看 imgur.com 上的帖子它对我有效:
library(sf)
library(geobr)
brasil <- read_state(year = 2020, showProgress = FALSE)
unidades_lat_long <- structure(
list(
geometry = structure(
[...]
)
library(ggplot2)
ggplot(brasil) +
geom_sf(aes(geometry = geom), fill="white") +
labs(title = "Unidades fixas - Brasil") +
geom_sf(data = unidades_lat_long, aes(geometry = geometry), fill = "black", shape = 18, size = 3) +
theme(legend.position = "none",
plot.title = element_text(hjust = 0.5),
plot.caption = element_text(hjust = 0, face = "italic", lineheight = 1),
axis.title=element_blank(),
axis.text=element_blank(),
axis.ticks=element_blank())+
ggmap::theme_nothing(legend = T)
但是我建议使用{tmap}包,它是专门用来绘制地图的。
tmap::tm_shape(brasil) +
tmap::tm_polygons(fill = "white") +
tmap::tm_shape(unidades_lat_long) +
tmap::tm_dots(size = 0.6, shape = 18) +
tmap::tm_title("Unidades fixas - Brasil", position = tmap::tm_pos_in("right", "TOP"), frame = FALSE)
创建于 2024 年 11 月 4 日,使用 reprex v2.1.1