使用ggplot2 / ggrepel绘制多边形外部点的标签

时间:2018-05-28 04:35:10

标签: r

我正在尝试使用ggplot2绘制我的研究区域的地图,因为我看到了一些好的输出。我附上了以下代码所得到的内容

可以使用此link下载shapefile。

library(ggplot2)
library(ggrepel)
points <- structure(list(city = structure(
  c(
    9L, 1L, 1L, 1L, 7L, 1L, 3L, 1L, 1L, 1L, 2L, 1L, 1L, 8L, 1L, 1L, 5L, 1L, 4L, 1L, 1L, 6L), 
  .Label = c("", "Adelaide", "Brisbane", "Canberra", "Darwin", "Hobart", "Melbourne", "Perth", "Sydney"), 
  class = "factor"), 
  site = structure(c(19L, 20L, 21L, 22L, 14L, 15L, 5L, 6L, 7L, 8L, 2L, 3L, 4L, 16L, 17L, 18L, 12L, 13L, 9L, 10L, 11L, 1L), 
                   .Label = c("", "ADL1", "ADL2", "ADL3", "BNE1", "BNE2", "BNE3", "BNE4", "CBR1", "CBR2", "CBR3", "DRW1", "DRW2", "MEL1", "MEL2", "PER1", "PER2", "PER3", "SYD1", 
                              "SYD2", "SYD3", "SYD4"), class = "factor"), 
  station = structure(c(8L, 5L, 12L, 16L, 10L, 3L, 22L, 18L, 17L, 20L, 2L, 14L, 11L, 4L, 7L, 19L, 15L, 21L, 6L, 9L, 13L, 1L),
                      .Label = c("", "Adelaide CBD", "Alphington", "Caversham", "Chullora", "Civic", "Duncraig", "Earlwood", "Florey", "Footscray", "Le Fevre 2", "Liverpool", "Monash", "Netley", "Palmerston", "Richmond", "Rocklea", "South Brisbane", "South Lake", "Springwood", "Winnellie", "Woolloongabba"), class = "factor"),
  latitude = c(-33.9178, -33.8939, -33.9328, -33.6183, -37.8048, -37.7783, -27.4975, -27.4848, -27.5358, -27.6125, -34.9289, -34.9438, -34.7913, -31.9505, NA, NA, -12.50779, -12.4243233, -35.285307, -35.220606, -35.418302, -42.8821), 
  longitude = c(151.1347, 151.045, 150.9058, 150.7458, 144.8727, 145.0306, 153.035, 153.0321, 152.9934, 153.1356, 138.6011, 138.5492, 138.498, 115.8605, NA, NA, 130.94853, 130.8933502, 149.131579, 149.043539, 149.094018, 147.3272)), 
  .Names = c("city", "site", "station",
             "latitude", "longitude"), class = "data.frame", row.names = c(NA, -22L)) 


AUS<-readRDS("gadm36_AUS_1_sp.rds")
ggplot() + geom_polygon(data = AUS, aes(x=long, y = lat, group = group, size=0.01), 
                        fill = NA, color = "black") + 
          geom_point(data=points, aes(x=longitude, y=latitude), color ="blue", size=0.5) + 
          coord_fixed(1) + 
          geom_label_repel(data = points, aes(x=longitude, y=latitude, label=city), 
                           box.padding = 1.2, point.padding = 1) +
         theme_classic() + ylim(-60,0)+ xlim(100,180) + scale_size_identity()

澳大利亚地图和我的学习领域:

enter image description here

有没有办法可以放大/扩展多边形,这样我的点就会分开?我也可以指定多边形外的所有标签吗?

0 个答案:

没有答案