【发布时间】:2026-01-05 21:55:01
【问题描述】:
我目前正在学习 R,我正在努力弄清楚如何在我用 R 制作的地图上的空间数据点上定义两种颜色。 In progress map
在数据框中有三个变量,x和y坐标,然后观察。 0 表示未观察,1 表示观察。我希望为观察到的 (1) 分配紫色,为未观察到的 (0) 分配红色。
这是迄今为止我用来制作地图的代码:
plot(Bp_shp4, col = "grey", axes = TRUE, xlab = "Longtitude", ylab = "纬度", xlim = c(-1, 35), ylim = c(35, 60), main = expression(''italic('Brachytron pretense')'存在或不存在的采样点'))
绘图(Bp_hydro,pch = 20,col =“青色”,cex = 0.5,add = TRUE)
绘图(Bp_Spatial_df, pch = 20, cex = 1, add = TRUE,)
Bp_Spatial_df 是我的具有三个变量的数据框,但是如果我指定一种颜色,它只会为一种一致的颜色着色,我尝试将其作为一个因素,但如果没有点不绘图,就无法将其添加到我现有的代码中。
任何帮助将不胜感激,我的任务已经完全停止,这几乎只是它的开始......
我项目中所有数据框的Str(data):
已加载 .CSV:
> str(Bp_Coord)
'data.frame': 93 obs. of 3 variables:
$ x : num 7.29 9.88 1.12 -3.88 22.21 ...
$ y : num 43.9 54 49.3 43.1 40.6 ...
$ Observed: int 1 1 1 1 1 1 1 1 1 0 ...
**Spatial point assignment**
coords <- SpatialPoints(Bp_Coord[, c("x", "y")])
> str(coords)
Formal class 'SpatialPoints' [package "sp"] with 3 slots
..@ coords : num [1:93, 1:2] 7.29 9.88 1.12 -3.88 22.21 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : NULL
.. .. ..$ : chr [1:2] "x" "y"
..@ bbox : num [1:2, 1:2] -9.12 37.21 26.46 58.62
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:2] "x" "y"
.. .. ..$ : chr [1:2] "min" "max"
..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
.. .. ..@ projargs: chr NA
空间点数据框代码:
Bp_Spatial_df <- SpatialPointsDataFrame(coords, Bp_Coord)
proj4string(Bp_Spatial_df) <- CRS("+proj=longlat +ellps=WGS84")
> str(Bp_Spatial_df)
Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
..@ data :'data.frame': 93 obs. of 3 variables:
.. ..$ x : num [1:93] 7.29 9.88 1.12 -3.88 22.21 ...
.. ..$ y : num [1:93] 43.9 54 49.3 43.1 40.6 ...
.. ..$ Observed: int [1:93] 1 1 1 1 1 1 1 1 1 0 ...
..@ coords.nrs : num(0)
..@ coords : num [1:93, 1:2] 7.29 9.88 1.12 -3.88 22.21 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : NULL
.. .. ..$ : chr [1:2] "x" "y"
..@ bbox : num [1:2, 1:2] -9.12 37.21 26.46 58.62
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:2] "x" "y"
.. .. ..$ : chr [1:2] "min" "max"
..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
.. .. ..@ projargs: chr "+proj=longlat +ellps=WGS84"
其他两个数据框只是 shapefile。
【问题讨论】:
-
您需要使用颜色的矢量 - 您正在绘制的每个点使用一种颜色。
标签: r plot colors mapping spatial