我正在尝试在ggplot2的地图中添加一些详细信息,但是我是该软件包的新手,因此不确定如何进行操作。代码和地图在下面(忽略地图上怪异的间距,我在Rstudio中进行了截屏)。感谢您的关注!
我正在尝试执行以下操作:
这是当前地图供参考的外观:
library(raster)
library(ggplot2)
library(ggthemes)
library(ggsn)
library(ggmap)
library(maps)
library(mapdata)
mapdata <- getData("GADM", country = "panama", level = 1)
mymap <- fortify(mapdata)
mypoint <- data.frame(long=c(-79.743, -79.696, -79.645, -79.595),
lat=c(9.160, 9.117, 9.058, 9.015),
group=c("L", "GW", "OGR", "LC"))
mypoint2 <- data.frame(long=c(-79.846, -79.707, -79.665, -79.610),
lat=c(9.181, 9.112, 9.057, 9.014),
group=c("BCI", "G", "EH", "MF"))
g1 <- ggplot() +
geom_blank(data = mymap, aes(x=long, y=lat)) +
geom_map(data = mymap, map = mymap,
aes(group = group, map_id = id),
fill = "#b2b2b2", color = "black", size = 0.3) +
coord_sf(xlim=c(-80,-79.5), ylim=c(8.9, 9.25), expand = FALSE) +
geom_point(data = mypoint, aes(x = long, y = lat),
color = "black", size = 3) +
geom_label(data = mypoint, aes(label = group, x = long, y = lat),
size = 3, fontface = "bold", nudge_x = c(0.015, 0.02, 0.022, 0.018)) +
geom_point(data = mypoint2, aes(x = long, y = lat),
color = "blue", size = 3) +
geom_label(data = mypoint2, aes(label = group, x = long, y = lat),
size = 3, fontface = "bold", nudge_x = c(-0.02, -0.018, -0.02, -0.02)) +
scale_x_continuous(limits = c(-80,-79.5), expand = c(0, 0)) +
scale_y_continuous(limits = c(8.9, 9.25), expand = c(0, 0)) +
theme_map() +
ggsn::scalebar(location = "bottomleft", dist = 5,
transform = TRUE, dist_unit = "km", model = 'WGS84',
x.min = -79.97, x.max = -79.8,
y.min = 8.93, y.max = 9.25) +
north(x.min = -79.6, x.max = -79.5,
y.min = 9.2, y.max = 9.24,
location = "toprgiht", scale = 0.1)
g2 <- ggplotGrob(
ggplot() +
geom_polygon(data = mymap,
aes(x = long, y = lat, group = group),
fill = "#b2b2b2", color = "black", size = 0.3) +
geom_point(data = mypoint, aes(x = long, y = lat),
color = "red", size = 0.5) +
coord_map("polyconic") +
theme_map() +
theme(panel.background = element_rect(fill = NULL))
)
g3 <- g1 +
annotation_custom(grob = g2, xmin = -79.75, xmax = -79.51,
ymin = 8.9, ymax = 9.0)
g3
答案 0 :(得分:1)
theme_map()
,纬度和经度刻度线消失了-它将axis_ticks
和axis_text
(除其他事项外)设置为element_blank()
。找回它们的一种方法是使用{li>覆盖theme_map()
g1_with_lbls <- g1 +
theme(
axis.text = element_text(),
axis.ticks = element_line(),
axis.title = element_text()
) +
xlab("Longitude") +
ylab("Lattitude")
ggplotGrob
对象的一部分g2 <- ggplotGrob(
ggplot() +
geom_polygon(data = mymap,
aes(x = long, y = lat, group = group),
fill = "#b2b2b2", color = "black", size = 0.3) +
geom_point(data = mypoint, aes(x = long, y = lat),
color = "red", size = 0.5) +
coord_map("polyconic") +
theme_map() +
theme(
panel.background = element_rect(fill = NULL)
) +
geom_rect(
aes(xmin = -80, xmax = -79, ymin = 8.5, ymax = 9.5), fill = NA,
col = "red", size = 1
)
)
然后
g3 <- g1_with_lbls +
annotation_custom(grob = g2, xmin = -79.75, xmax = -79.51,
ymin = 8.9, ymax = 9.0)
g3
同时具有刻度和矩形。