如何固定轴并在ggplot2中将多边形添加到插图中

时间:2019-11-19 06:55:22

标签: r ggplot2 mapping

我正在尝试在ggplot2的地图中添加一些详细信息,但是我是该软件包的新手,因此不确定如何进行操作。代码和地图在下面(忽略地图上怪异的间距,我在Rstudio中进行了截屏)。感谢您的关注!

我正在尝试执行以下操作:

  1. 在x和y轴上添加纬度和经度刻度线。我在下面的脚本中编写了代码,但是什么也没发生,不知道为什么-没有错误弹出。
  2. 如果可能的话,我想在插图中添加一个框来显示较大地图的范围/覆盖范围,因为较小地图中的点(红色的点)几乎看不见。我不确定如何在插图中添加和放置多边形。

这是当前地图供参考的外观:

panama map

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 

1 个答案:

答案 0 :(得分:1)

  1. 由于theme_map(),纬度和经度刻度线消失了-它将axis_ticksaxis_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")
  1. 您应将多边形放置在较小的地图上,作为要创建的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 

plot with ticks and a rectangle

同时具有刻度和矩形。