将世界地图划分为特定国家/地区的小组

时间:2014-01-15 13:51:40

标签: r map ggplot2

我有以下几点:

> dd
           area         Lon       Lat
1    2530000.00  -84.744000  39.57500
2    5300000.00   -3.200000  54.66000
3     100000.00   -2.970000  54.40000
4      10632.91  -89.494500  46.23046
5     190000.00   -2.923600  54.50630
6     239000.00   -3.260200  54.42850
7     930000.00   -3.264400  54.53120
8     223045.80   12.303000  55.93400
9    3236000.00   -3.691800  52.94930
10     94000.00   -3.028400  56.49910
11   4700000.00   -3.075300  54.33810
12    400000.00   -3.819200  52.99930
13    258924.72  -89.473300  46.21046
14    340000.00  -89.612000  46.00200
15      5524.00  -89.606000  46.00800
16     57000.00   -3.962000  52.79470
17    170000.00   -3.025900  54.42720
18   3000000.00   -3.381200  54.52220
19  23674200.00   18.587000  59.84600
20   1000000.00   -2.990000  54.36000
21    140000.00   -3.990100  57.54240
22    110000.00    9.452022  56.01752
23    640000.00   -3.019500  54.44840
24    100285.00   12.303000  55.98500
25    664549.00    9.391000  56.02000
26   1513724.00  -79.135600  45.37960
27      7862.06  -89.505900  46.24369
28  60900000.00   10.070000  45.72400
29 163268000.00   35.588000  32.81700
30      2468.00  -89.678000  45.99500
31     51000.00  -85.349000  42.41100
32     51000.00   -4.041800  53.01420
33   1057000.00   -4.546000  56.21300
34     93000.00   -5.429600  57.49210
35   1114000.00   -4.281000  55.00210
36     99000.00   -3.321500  56.95920
37    111000.00   -4.509200  56.22730
38  71000000.00   -4.616000  56.10500
39    600000.00   -3.350000  54.58000
40  38282400.00  -89.424000  43.10500
41     58818.50  -89.521300  46.25745
42     37833.00  -89.722000  45.99800
43      4624.00  -89.705000  46.00500
44    970000.00   -4.131900  53.12800
45      3000.00          NA        NA
46     10000.00   -2.990800  54.37210
47    127000.00   -4.430400  55.09330
48  79855500.00  176.267000 -38.08200
49  34600000.00  176.419000 -38.03700
50    500000.00   -2.386400  53.35400
51  19800000.00 -122.094000  47.60000
52     35500.00   -5.448100  57.27850
53    100500.00   -5.445000  57.59120
54     96400.00   -5.392200  57.51680
55     14400.00   -5.305400  57.25070
56     16400.00   -5.103700  57.17190
57    311000.00   -5.024400  57.71260
58     75500.00   -4.954900  57.49550
59     53600.00   -4.936500  57.45240
60    216100.00   -4.954400  57.68130
61     56000.00   -4.935600  57.80980
62    144600.00   -3.538200  56.88500
63     43000.00   -3.299100  54.48120
64  14918400.00  -89.672000  46.02900
65    618453.00  -89.700000  46.00800
66    820000.00   -4.031300  55.79300
67   4100000.00    3.630000  52.88000
68       566.00  -89.431000  46.00300
69     10478.00  -89.686000  46.04100
70   8900000.00   -2.889000  54.57610
71   2350000.00 -122.335100  47.63900
72  54000000.00  177.088000 -38.76800
73  88000000.00 -122.257000  47.61800
74     48657.67  -89.502830  46.23562
75   6700000.00   -2.930000  54.35000
76   1300000.00  -89.417800  43.05370
77    164000.00  -85.383000  42.39800

我用这些点生成了这些点的世界地图:

library(ggplot2)
library(maps)
mdat <- map_data('world')

str(mdat)
ggplot() + 
  geom_polygon(dat=mdat, aes(long, lat, group=group), fill="grey50") +
  geom_point(data=dd, 
             aes(x = Lon, y = Lat, size=`area`), col="red")

导致:

enter image description here

然而,从这里,我可以看到6个不同的区域:
1.华盛顿2.大湖区3.英国。 4.新西兰5.欧洲其他国家。 6.以色列

将此地图拆分为6个面板以更好地展示地区的最佳方法是什么。注意,红色圆圈表示特定变量的不同大小,并且我希望在面板之间携带大小,即不是每个面板的单独图例。

我最终希望产生类似

的东西

enter image description here

尽管每个小组应代表世界上不同的区域,如上所述。

1 个答案:

答案 0 :(得分:2)

不幸的是,由于这些地图,ggplot上的自由尺度不起作用。我不知道为每个方面手动指定比例的任何方法,因此有三种可能的解决方案:

  1. 只要求ggplot绘制您感兴趣的区域。这很费力,但有效(请参阅此处的解决方案)
  2. 按区域循环多个ggplot绘图并手动指定刻度,然后重新组合(使用grid或手动)
  3. 最优雅的是获取地图数据,然后“切割它”,只留下你想要的区域,多边形适当地关闭。
  4. 虽然3.会更好,但是用于cookie切割的算法可能比上面的hackish#1解决方案花费的时间更长,所以这里是#1:

    library(ggplot2)
    library(maps)
    df <- df[complete.cases(df),]
    df$dist.london <- sqrt((df$Lat - 51.5) ^ 2 + (df$Lon - 0.1275) ^ 2)
    df$rg <- cut(df$dist.london, c(0, 10, 35, 65, 100, 150, 300), include.lowest=T)
    levels(df$rg) <- c("UK", "RoE", "Israel", "Midwest", "US West", "New Zealand")
    
    RoE <- c("France", "Germany", "Denmark", "Spain", "Italy", "Belgium", "Netherlands")
    West <- tolower(c("Washington", "Oregon", "California", "Nevada", "Idaho"))
    Lakes <- tolower(c("Michigan", "Illinois", "Wisconsin", "Indiana", "Ohio", "New York"))
    mdat <- rbind(
      map_data('world', c("UK", RoE, "Israel", "New Zealand")),
      map_data("state", c(West, Lakes))
    )
    reg.map <- rbind(
      cbind(RoE, "RoE"),
      cbind(West, "US West"),
      cbind(Lakes, "Midwest"),
      cbind("UK", "UK"),
      cbind("Israel", "Israel"),
      cbind("New Zealand", "New Zealand")
    )
    mdat$rg <- factor(reg.map[match(mdat$region, reg.map[, 1]), 2], levels=levels(df$rg))
    mdat <- subset(mdat, !(rg == "RoE" & (lat < 35 | long < -20)))
    mdat <- subset(mdat, !(rg == "UK" & (lat < 50 | long < -20)))
    mdat <- subset(mdat, !(rg == "New Zealand" & (lat > -20 | long < 100)))
    
    ggplot(data=df) + 
      geom_polygon(dat=mdat, aes(long, lat, group=group), fill="grey50", color="grey10") +
      geom_point(aes(x = Lon, y = Lat, size=`area`), col="red") +
      facet_wrap(~ rg, scales="free")
    

    enter image description here

    我对大湖区和RoE很懒,但你明白了。