【发布时间】:2020-05-12 17:50:13
【问题描述】:
我遇到了一个 ggplot2 问题。这次我使用两个数据集在 ggplot2 中绘制它们(1=6 个多边形重叠在一起,2=5 个 GPS 点)。我可以分别绘制它们,但不能在同一个图中一起绘制。
每次我这样做时,都会收到以下错误代码:FUN(X[[i]], ...) 中的错误:找不到对象“HR”
如果有人可以再看一下,我将非常感激!谢谢。
代码在这里
library(ggplot2)
library(sf)
library(sp)
library(viridis)
library(ggnewscale)
#they plot well separately
poly_sf4<-st_as_sf(poly_sf3)
ggplot(poly_sf4) +
geom_sf()
roost_sf3<- st_as_sf(roost_sf2)
ggplot(roost_sf3) +
geom_sf()
#but not together
theme_set(theme_light())
june_only <- ggplot(subset(poly_sf4, Sequence %in% 1),#the data is the sf object,
#set aesthetics based on the other columns/vars in the sf df
aes(color = HR, fill = HR, alpha = HR)) +
geom_sf(size = 1) +
scale_alpha_manual(name = "HR:",
values = c(0.2,0.4)) +
scale_color_manual(name = "HR:",
values = viridis(3)[1:2]) +
scale_fill_manual(name = "HR:",
values = viridis(3)[1:2]) +
theme(legend.position="bottom",
legend.text = element_text(size = 15),
legend.title = element_text(size = 15),
legend.key.size = unit(0.5, "cm"),
text = element_text(size=10))
june_only_roost <- june_only +
new_scale_colour() +
geom_sf(data=roost_sf3, aes(colour=Existing), size=3) +
scale_color_manual(name = "Existing:",
values = viridis(10)[5:7])
june_only_roost
#Error in FUN(X[[i]], ...) : object 'HR' not found
数据
#Data 01: Polygons
poly_sf3 <- structure(list(id = "NSF", area = 18.3641861686707, Month = "01: Jun 2019",
geometry = structure(list(structure(list(structure(c(365367.33618389,
365264.754631496, 364731.911588065, 364810.96801954, 365050.261906325,
365346.205227799, 365367.33618389, 155179.53820745, 154761.691240949,
155005.189675172, 155114.59881625, 155249.352539069, 155198.343585666,
155179.53820745), .Dim = c(7L, 2L))), class = c("XY", "POLYGON",
"sfg"))), class = c("sfc_POLYGON", "sfc"), precision = 0, bbox = structure(c(xmin = 364731.911588065,
ymin = 154761.691240949, xmax = 365367.33618389, ymax = 155249.352539069
), class = "bbox"), crs = structure(list(input = "EPSG:32648",
wkt = "PROJCS[\"WGS 84 / UTM zone 48N\",\n GEOGCS[\"WGS 84\",\n DATUM[\"WGS_1984\",\n SPHEROID[\"WGS 84\",6378137,298.257223563,\n AUTHORITY[\"EPSG\",\"7030\"]],\n AUTHORITY[\"EPSG\",\"6326\"]],\n PRIMEM[\"Greenwich\",0,\n AUTHORITY[\"EPSG\",\"8901\"]],\n UNIT[\"degree\",0.0174532925199433,\n AUTHORITY[\"EPSG\",\"9122\"]],\n AUTHORITY[\"EPSG\",\"4326\"]],\n PROJECTION[\"Transverse_Mercator\"],\n PARAMETER[\"latitude_of_origin\",0],\n PARAMETER[\"central_meridian\",105],\n PARAMETER[\"scale_factor\",0.9996],\n PARAMETER[\"false_easting\",500000],\n PARAMETER[\"false_northing\",0],\n UNIT[\"metre\",1,\n AUTHORITY[\"EPSG\",\"9001\"]],\n AXIS[\"Easting\",EAST],\n AXIS[\"Northing\",NORTH],\n AUTHORITY[\"EPSG\",\"32648\"]]"), class = "crs"), n_empty = 0L),
Sequence = 1, HR = "MCP"), sf_column = "geometry", agr = structure(c(id = NA_integer_,
area = NA_integer_, Month = NA_integer_, Sequence = NA_integer_,
HR = NA_integer_), .Label = c("constant", "aggregate", "identity"
), class = "factor"), row.names = 11L, class = c("sf", "data.frame"
))
#Data 02: 5 gps points
roost_sf2 <- new("SpatialPointsDataFrame", data = structure(list(Name = c("Ro-Giraffe",
"Ro-Cape Buffalo", "Ro-MLR", "Ro-Night Safari Entrance (felled)",
"Ro-Elephant", "Ro-Tram Station"), descriptio = c(NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_
), timestamp = c(NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_), begin = c(NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_
), end = c(NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_), altitudeMo = c(NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_
), tessellate = c("-1", "-1", "-1", "-1", NA, NA), extrude = c("0",
"0", "0", "0", NA, NA), visibility = c("-1", "-1", "-1", "-1",
NA, NA), drawOrder = c(NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_), icon = c(NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_
), Existing = c("Yes", "Yes", "No", "No", "Yes", "New")), class = "data.frame", row.names = c(NA,
-6L)), coords.nrs = numeric(0), coords = structure(c(103.789426,
103.786853, 103.787402, 103.788971, 103.785468833941, 103.788364169811,
1.399106, 1.401148, 1.404815, 1.404159, 1.40226000499339, 1.40343003798172,
0, 0, 0, 0, 0, 0), .Dim = c(6L, 3L), .Dimnames = list(NULL, c("coords.x1",
"coords.x2", "coords.x3"))), bbox = structure(c(103.785468833941,
1.399106, 0, 103.789426, 1.404815, 0), .Dim = 3:2, .Dimnames = list(
c("coords.x1", "coords.x2", "coords.x3"), c("min", "max"))),
proj4string = new("CRS", projargs = "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))
【问题讨论】:
-
我无法读取
roost_sf2数据并且出现错误。使用reprex包发布问题/答案是一种很好的做法,因为它可以确保代码适用于任何其他 R 实例。 -
nsf_roost_sf2 未提供。第一部分“june_only”对我来说很好用。
-
更新代码!对困惑感到抱歉。 @Rhino8
-
我认为你需要
sp库来阅读roost_sf2