r - geom_sf和geom_point,如何根据地图上插入的点将颜色分配给图例中的对应类型?
问题描述
我需要使用 ggplot 在地图上插入多个点。我想在图例中显示与在 points$types 中注册的类型相对应的颜色和形状的数量。我还需要将 point$obj 的值与坐标的插入一起打印在地图上。但是,我设法获得了该代码,但我无法分配颜色和形状。如何解决这个问题呢?下面是一个绘图应该是什么样子的示例:彩色坐标和打印标签:
这是代码:
library(geobr)
library(ggspatial)
library(ggplot2)
BR <- read_state(year=2018)
points
obj x y types
1 F1 -43.18669 -22.901724 A
2 F2 -43.31534 -22.779600 A
3 F3 -67.82527 -9.984939 B
4 F4 -72.74519 -7.610681 B
5 F5 -35.93844 -9.308399 B
6 F6 -63.13576 -4.105584 B
7 F7 -60.00568 -2.049304 B
8 F8 -35.91194 -7.217137 C
9 F9 -35.04254 -7.998586 C
10 F10 -48.26501 -18.889202 C
11 F11 -45.23610 -21.238526 D
12 F12 -43.71210 -22.244824 E
# plot
ggplot() +
geom_sf(
data=BR,
color="black",
size=.1, show.legend = T) +
geom_point(data=points,
aes(x=x, y= y, fill = as.factor(types)),
size = 3) +
scale_fill_manual(values=c("#999999","#000000", "#E69F00", "#56B4E9","#3399FF"))+
labs(x="Longitude", y="Latitude", fill = "Types", size=8 ) +
theme_bw()
解决方案
这是你想要的吗?
library(geobr)
library(ggspatial)
library(ggplot2)
library(ggrepel)
library(sf)
BR <- read_state(year=2018)
# plot
ggplot() +
geom_sf(data=BR,
color="black",
size=.1, show.legend = T) +
geom_point(data=points,
aes(x=x, y= y, color = types, size = types)) +
scale_color_manual(values=c("#999999","#000000", "#E69F00", "#56B4E9","#3399FF")) +
labs(x="Longitude", y="Latitude", color = "Types", size="Types") +
geom_label_repel(data=points, aes(x=x, y= y, label=obj)) +
theme_bw()
要添加首都的名称,首先,我们需要计算每个状态多边形的质心作为绘制它们名称的坐标。
BR <- cbind(BR, st_coordinates(st_centroid(BR)))
它会给出一个警告,基本上说使用经度/纬度数据(即 WGS84)的质心坐标不准确,这对于绘图目的来说非常好。然后使用下面的代码
ggplot() +
geom_sf(data=BR,
color="black",
size=.1, show.legend = T) +
geom_text(data = BR, aes(X, Y, label = abbrev_state), colour = "black") +
geom_point(data=points,
aes(x=x, y= y, color = types, size = types)) +
scale_color_manual(values=c("#999999","#000000", "#E69F00", "#56B4E9","#3399FF")) +
labs(x="Longitude", y="Latitude", color = "Types", size="Types") +
geom_label_repel(data=points, aes(x=x, y= y, label=obj)) +
theme_bw()
points <- structure(list(sl = 1:12, obj = c("F1", "F2", "F3", "F4", "F5",
"F6", "F7", "F8", "F9", "F10", "F11", "F12"), x = c(-43.18669,
-43.31534, -67.82527, -72.74519, -35.93844, -63.13576, -60.00568,
-35.91194, -35.04254, -48.26501, -45.2361, -43.7121), y = c(-22.901724,
-22.7796, -9.984939, -7.610681, -9.308399, -4.105584, -2.049304,
-7.217137, -7.998586, -18.889202, -21.238526, -22.244824), types = c("A",
"A", "B", "B", "B", "B", "B", "C", "C", "C", "D", "E")), class = "data.frame", row.names = c(NA,
-12L))
推荐阅读
- python-3.x - *** `python3' 中的错误:free():无效指针:0xb70c3578 *** 中止(核心转储)
- python - 循环遍历多个列表?
- amazon-web-services - AWS 弹性搜索服务 - IAM 角色(刷新会话令牌 - Node.js)
- uwp - UWP _mediaCapture.StartPreviewAsync() System.Runtime.InteropServices.COMException (0xC00D36B2)
- ansible - Ansible 条形空白
- php - 动态 SQL 命令
- python - 在 Sympy 中,什么相当于 Mathematica 的符号化最小化函数?
- c# - 单击按钮后更改 SQL 数据网格视图
- sql - SQL DB2 查找和显示通用
- loadlibrary - 桌面程序无权加载uwp app目录下的库dll,所有路径:C:\Program Files\WindowsApps\xxxx\xxxx.dll