无法使用Terra r Pakage为点Shapefile添加传奇

发布于 2025-02-08 18:47:38 字数 967 浏览 2 评论 0原文

我想添加绘制在栅格顶部的点shapefile的传奇。我正在使用以下代码

library(terra)

f <- system.file("ex/elev.tif", package="terra") 
r <- rast(f)
plot(r)

d <- data.frame(lon=c(6.0,6.1,6.2,6.2), 
                lat = c(49.7,49.8,49.9,49.6),
                Class=c("Yes", "No"))

m <- vect(d, geom=c("lon", "lat"), crs="+proj=longlat +datum=WGS84")

plot(m, add = T, col = c("red", "blue"), legend=TRUE,
     plg=list(legend=c("No", "Yes")))

”在此处输入图像描述”

您可以从传说所缺少的输出中看到。如果我添加了传奇功能,

legend("bottomright", legend=names(table(points$Class)), col=c("red", "blue"))

则添加了颜色的传奇。

我如何在传奇中拥有各自的颜色?

I want to add legend for point shapefile plotted on top of a raster. I am using the following code

library(terra)

f <- system.file("ex/elev.tif", package="terra") 
r <- rast(f)
plot(r)

d <- data.frame(lon=c(6.0,6.1,6.2,6.2), 
                lat = c(49.7,49.8,49.9,49.6),
                Class=c("Yes", "No"))

m <- vect(d, geom=c("lon", "lat"), crs="+proj=longlat +datum=WGS84")

plot(m, add = T, col = c("red", "blue"), legend=TRUE,
     plg=list(legend=c("No", "Yes")))

enter image description here

As you can see from the output that the legend is missing. If I add a legend function like

legend("bottomright", legend=names(table(points$Class)), col=c("red", "blue"))

It adds a legend withou the colours.
enter image description here

How can I have the respective colours in the legend?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫南 2025-02-15 18:47:38

您需要告诉绘图函数要使用哪个图层:

plot(m, y="Class", add=T, col=c("red", "blue"), legend="bottomright")

You need to tell the plot function which layer to use:

plot(m, y="Class", add=T, col=c("red", "blue"), legend="bottomright")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文