如何阻止 geom_point 删除行以创建地图

How can I stop geom_point from removing rows in order to create a map

我的意图是在地图上绘制几个我有经度和纬度的位置(作为简单的点)。这些地点分布在乌干达各地。

print(locations)
      Latitude   Longitude
1     0.482980   30.212160
2     0.647717   30.315984
3      0.44735    30.18063
4   0.58416316  30.2066327
5      0.60012    30.19998
6     0.433483    30.20179
7     0.625317   30.224837
8     0.654277   30.251667
9     0.387517   30.197475
10    0.607402   30.292068
11    0.770128   30.403456
12    0.767266   30.414246
13    0.777873   30.389111
14    0.631774   30.290356
15    0.734015   30.279161
16    0.722133   30.277941
17  0.66322994 30.22795225
18  0.66900827 30.21357739
19    0.450372   30.197764
20    0.493699   30.250891
21    0.479716   30.180958
22    0.483242   30.284576
23    0.645044   30.321270
24    0.602389   30.275637
25    0.868827   30.465939
26    0.631194   30.263565
27    0.631576   30.263855
28    0.413701   30.247934
29     0.67135     30.2675
30    0.492360   30.223620
31     0.81481    30.39311
32    0.396665    30.26309
33    0.666170   30.308960
34    0.610067   30.306058
35    0.677144   30.196810
36    0.677144   30.196810
37    0.555555   30.231681
38     0.63874   30.231691
39    0.512953   30.207603
40    0.442291   30.279173
41    0.575658   30.310231
42    0.423129   30.211289
43    0.623838   30.256925
44    0.639643   30.341620
45    0.653550   30.170428
46    0.752630   30.401040
47    0.478544   30.191938
48     0.48114   30.198471
49    0.679820   30.259800
50    0.581293   30.158619
51    0.730410   30.376620
52    0.504059   30.178556
53    0.587441   30.310364
54    0.588072   30.277877
55  0.70893233 30.19008103
56     0.81699    30.41799
57    0.609300   30.271613
58    0.595226   30.315580
59    0.459029   30.277659
60    0.727873   30.216385
61    0.647722   30.217760
62    0.690064   30.193881
63    0.512339   30.140107
64    0.649181   30.302570
65    0.649881   30.303974
66    0.649736   30.302481
67    0.722082   30.226063
68    0.463480   30.203050
69    0.692930   30.281880
70    0.652864   30.229106
71    0.491520   30.233780
72    0.778370   30.415920
73    0.682090   30.276460
74    0.564670   30.148920
75    0.655588   30.243047
76    0.647717   30.315984
77    0.518769   30.159384
78    0.683070   30.339650
79    0.662980   30.253890
80    0.591899   30.145857
81    0.699690   30.344650
82    0.441030   30.177240
83    0.612202   30.213022
84    0.472530   30.236980
85    0.473722   30.165020
86    0.499181   30.159485
87   0.6598021    30.29158
88   0.6601362    30.29119
89     0.48386   30.23142 
90    0.679470   30.282190
91    0.685860   30.271070
92    0.528797   30.171251
93    0.514863   30.243976
94    0.603612   30.258705
95    0.484708   30.142588
96    0.523857   30.233239
97    0.395356   30.215351
98    0.612247   30.269341
99  0.55878815 30.17702095
100   0.747630   30.384240
101   0.538778   30.326353
102   0.554198   30.299815
103   0.504410   30.298260
104   0.418705   30.259747
105   0.669850   30.324100
106   0.654277   30.251667
107   0.460830   30.214070
108   0.378725   30.216429

这是我到目前为止设法做到的:

locations$Latitude=as.numeric(levels(locations$Latitude))[locations$Latitude]
locations$Longitude=as.numeric(levels(locations$Longitude))[locations$Longitude]

uganda <- raster::getData('GADM', country='UGA', level=1) 

ggplot() +
  geom_polygon(data = uganda,
               aes(x = long, y = lat, group = group),
               colour = "grey10", fill = "#fff7bc") +
  geom_point(data = locations,
             aes(x = Longitude, y = Latitude)) +
  coord_map() +
  theme_bw() +
  xlab("Longitude") + ylab("Latitude")

正如您执行上面的代码所见,乌干达的地图是从 GADM 数据库加载并正确显示的。但是,我收到以下警告消息:

Warning:
Removed 108 rows containing missing values (geom_point). 

我在另一个 post () 中读到这个错误可能是由错误的轴范围引起的。不过,我不熟悉地理数据和 GADM 地图的绘制。这就是我无法调整范围的原因(我想这将在 geom_polygon 部分完成)。有人可以帮帮我吗?

我不确定你为什么要运行你的代码的第一部分:

locations$Latitude=as.numeric(levels(locations$Latitude))[locations$Latitude] locations$Longitude=as.numeric(levels(locations$Longitude))[locations$Longitude]

如果你不 运行 那部分,就不会有任何 NA 了。因此,如果您 运行 以下代码,它应该可以工作:

library(tidyverse)
library(raster)
uganda <- raster::getData('GADM', country='UGA', level=1) 

ggplot() +
  geom_polygon(data = uganda,
               aes(x = long, y = lat, group = group),
               colour = "grey10", fill = "#fff7bc") +
  geom_point(data = locations,
             aes(x = Longitude, y = Latitude)) +
  coord_map() +
  theme_bw() +
  xlab("Longitude") + ylab("Latitude")

输出: