如何通过ggplot2绘制ShapeFile?
我有一个栅格数据和公园多边形,我想将其重叠在光栅上。当我添加多边形时,它在此处显示,但是在GGPLOT上,我如何在我的栅格数据上通过ggplot2添加多边形(公园的多边形就像圆形形状)。我的代码附加在下面。
r <- raster(t((volcano[,ncol(volcano):1] - 94) * 4.95))
pg <- readOGR("E:/park/1aa.shp") # loadshapfile
plot(r)
plot(pg, add= TRUE,) # it appears here like first picture (left).
但是我该如何在我的ggplot 2中添加这个多边形o公园。我的GGPLOT 2代码在下面附有。
centile90 <- quantile(r, 0.90)
df <- as.data.frame(as(r, "SpatialPixelsDataFrame"))
colnames(df) <- c("value", "x", "y")
library(ggplot2)
mybreaks <- seq(0, 500, 50)
ggplot(df, aes(x, y, z = value)) +
geom_contour_filled(breaks = mybreaks) +
geom_contour(breaks = centile90, colour = "pink",
size = 0.5) +
scale_fill_manual(values = hcl.colors(length(mybreaks) - 3, "Zissou1", rev = FALSE)) +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +
theme_classic() +
theme()
需要帮助如何在我的ggplot2代码中添加** pg(polygon)**。
更新1 polygon数据的描述
I have a raster data and polygons of parks and I want to overlap it on the raster. When I add the polygon it shows here but on ggplot how I add polygons (polygons of parks is like round shapes)on my raster data through ggplot2,. My code is attached below.
r <- raster(t((volcano[,ncol(volcano):1] - 94) * 4.95))
pg <- readOGR("E:/park/1aa.shp") # loadshapfile
plot(r)
plot(pg, add= TRUE,) # it appears here like first picture (left).
But how can I add this polygons o parks in my ggplot 2. My code of ggplot 2 is attached below.
centile90 <- quantile(r, 0.90)
df <- as.data.frame(as(r, "SpatialPixelsDataFrame"))
colnames(df) <- c("value", "x", "y")
library(ggplot2)
mybreaks <- seq(0, 500, 50)
ggplot(df, aes(x, y, z = value)) +
geom_contour_filled(breaks = mybreaks) +
geom_contour(breaks = centile90, colour = "pink",
size = 0.5) +
scale_fill_manual(values = hcl.colors(length(mybreaks) - 3, "Zissou1", rev = FALSE)) +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +
theme_classic() +
theme()
Help is needed how to add ** pg (polygon) ** in my ggplot2 code.
Update 1
Description of polygon data
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如前所述,使用
sf
比使用sp
更方便,而且sf
旨在取代sp
代码>.在这里找到一个可重现的例子。第一部分仅用于模拟您的文件
“E:/park/1aa.shp”
。由于未提供,我无法使用您的真实数据,但我们假设它是相同的数据集...:现在使用
geom_sf()
在你的pg
对象:As explained, it is much handy to work with
sf
thansp
, on top of thatsf
is meant to superseedsp
.Find here a reproducible example. The first part is just for mocking your file
"E:/park/1aa.shp"
. Since it was not provided it was not possible for me to use your real data, but let's just pretend it is the same dataset...:Now work with
geom_sf()
on yourpg
object: