用标签绘制空间坐标

Top*_*ili 1 plot r spatial ggplot2

我在R中是个新手。我有许多座标,我想以适当的方式在R中绘制它们,并显示标签。此外,轴应呈现纬度和经度。我尝试了ggplot,但无法将数据放入代码中。

   id      lon      lat
1   2 7.173500 45.86880
2   3 7.172540 45.86887
3   4 7.171636 45.86924
4   5 7.180180 45.87158
5   6 7.178070 45.87014
6   7 7.177229 45.86923
7   8 7.175240 45.86808
8   9 7.181409 45.87177
9  10 7.179299 45.87020
10 11 7.178359 45.87070
11 12 7.175189 45.86974
12 13 7.179379 45.87081
13 14 7.175509 45.86932
14 15 7.176839 45.86939
15 17 7.180990 45.87262
16 18 7.180150 45.87248
17 19 7.181220 45.87355
18 20 7.174910 45.86922
19 25 7.154970 45.87058
20 28 7.153399 45.86954
21 29 7.152649 45.86992
22 31 7.154419 45.87004
23 32 7.156099 45.86983
Run Code Online (Sandbox Code Playgroud)

Pau*_*tra 5

为此,请使用geom_text几何体:

ggplot(aes(x = lon, y = lat), data = df) + geom_text(aes(label = id))
Run Code Online (Sandbox Code Playgroud)

该地块在文本id由列specfied的位置列lonlat。数据存储在中data.frame df

或使用:

ggplot(aes(x = lon, y = lat), data = df) + geom_text(aes(label = id)) + 
    geom_point()
Run Code Online (Sandbox Code Playgroud)

如果要同时添加点和标签。使用hjust和的vjust参数geom_text来更改标签相对于点的方向。另外,var通过使用美学中的color参数,根据列为每个点赋予颜色geom_point

ggplot(aes(x = lon, y = lat), data = df) + geom_text(aes(label = id)) + 
    geom_point(aes(color = var))
Run Code Online (Sandbox Code Playgroud)

请注意,ggplot2它不能处理软件包Spatial提供的类sp。使用as.data.frame转换点(SpatialPoints)和gridsets(SpatialPixels / SpatialGrid)到data.frame的。此外,还可fortify用于将面数据集(SpatialPolygons)转换为data.frame

  • ...添加为对问题的编辑。但是,我建议您阅读ggplot2,因为这些内容可以在ggplot2的文档中找到。 (2认同)