如何通过ggplot2绘制ShapeFile?

发布于 2025-01-19 11:01:49 字数 1388 浏览 5 评论 0原文

我有一个栅格数据和公园多边形,我想将其重叠在光栅上。当我添加多边形时,它在此处显示,但是在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).

enter image description here

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

enter image description here

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

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

发布评论

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

评论(1

寄风 2025-01-26 11:01:49

如前所述,使用 sf 比使用 sp 更方便,而且 sf 旨在取代 sp代码>.

在这里找到一个可重现的例子。第一部分仅用于模拟您的文件“E:/park/1aa.shp”。由于未提供,我无法使用您的真实数据,但我们假设它是相同的数据集...:

library(raster)
library(sf)
library(sp)

r <- raster(t((volcano[,ncol(volcano):1] - 94) * 4.95))

# Let's mock your shapefile
poly <- st_as_sfc(st_bbox(st_as_sf(rasterToPolygons(r))))

# Sample 4 points
set.seed(3456)

sample <- st_sample(poly, 4)
sample <- st_buffer(sample, c(0.01, 0.02, 0.03))
sample <- st_sf(x=1:4, sample)
st_write(sample, "1aa.shp", append = FALSE)
# Mocked data

# Now let's start with your code -------
library(raster)
library(sf)

r <- raster(t((volcano[,ncol(volcano):1] - 94) * 4.95))

# Use sf!!
pg <- st_read("1aa.shp") # loadshapfile 
plot(r)
plot(st_geometry(pg), add= TRUE,) # it appears here like first picture (left).

在此处输入图像描述

现在使用 geom_sf() 在你的pg 对象:



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) +
  # And here we have it
  geom_sf(data=pg, fill="black", inherit.aes = FALSE) +
   scale_fill_manual(values = hcl.colors(length(mybreaks)-1, "Zissou1", rev = FALSE)) +
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0)) +
  theme_classic() +
  theme()

在此处输入图像描述

As explained, it is much handy to work with sf than sp, on top of that sf is meant to superseed sp.

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...:

library(raster)
library(sf)
library(sp)

r <- raster(t((volcano[,ncol(volcano):1] - 94) * 4.95))

# Let's mock your shapefile
poly <- st_as_sfc(st_bbox(st_as_sf(rasterToPolygons(r))))

# Sample 4 points
set.seed(3456)

sample <- st_sample(poly, 4)
sample <- st_buffer(sample, c(0.01, 0.02, 0.03))
sample <- st_sf(x=1:4, sample)
st_write(sample, "1aa.shp", append = FALSE)
# Mocked data

# Now let's start with your code -------
library(raster)
library(sf)

r <- raster(t((volcano[,ncol(volcano):1] - 94) * 4.95))

# Use sf!!
pg <- st_read("1aa.shp") # loadshapfile 
plot(r)
plot(st_geometry(pg), add= TRUE,) # it appears here like first picture (left).

enter image description here

Now work with geom_sf() on your pg object:



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) +
  # And here we have it
  geom_sf(data=pg, fill="black", inherit.aes = FALSE) +
   scale_fill_manual(values = hcl.colors(length(mybreaks)-1, "Zissou1", rev = FALSE)) +
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0)) +
  theme_classic() +
  theme()

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文