【问题标题】:How to plot the shapefile via ggplot2?如何通过 ggplot2 绘制 shapefile?
【发布时间】:2022-08-14 09:55:23
【问题描述】:

我有一个栅格数据和公园的多边形,我想将它重叠在栅格上。当我添加多边形时,它会在此处显示,但在 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 中添加这个多边形公园。我的 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(多边形)** 需要帮助。

更新 1多边形数据说明

  • 这不是一个可重复的例子。请提供其他人可以运行的示例。我们无权访问这些数据。
  • 我已经进行了更改并尝试使其可重现,请查看它。
  • 因为sp 的开发人员基本上弃用了该软件包,并将所有新开发的重点放在较新的sf 软件包上,所以现在使用sf 而不是spggplot2 绘图上绘制多边形更容易。尝试使用sf::st_read() 而不是readOGR 读取您的shapefile,然后您可以将geom_sf() 添加到您的绘图中。见r-spatial.github.io/sf/articles/sf5.html
  • 我明白,但我的问题是我如何写作geom_sf()在我的ggplot代码中?我需要在哪一行代码中添加此函数以在光栅图像上绘制我的 shapefile。很抱歉英语和编程意识差@qdread。
  • 你能分享你的1aa.shp文件吗?所以我们可以重现您的问题。

标签: r ggplot2 polygon raster shapefile


【解决方案1】:

如前所述,使用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).

现在在您的pg 对象上使用geom_sf()



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()

【讨论】:

  • @diegernan 它真的对我有用。你能告诉我最后一件事,请告诉我如何在我的代码中手动添加不同的颜色。的配色方案齐苏1对我的数据不利。我想添加这个配色方案“#999999”、“#E69F00”、“#56B4E9”、“#009E73”、“#F0E442”、“#0072B2”、“#D55E00”、“#CC79A7”这些颜色。在这最后一件事上需要你的帮助
  • 当然,尝试使用scale_fill_manual(values = c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")),但要确保颜色数量与my_breaks 上的元素数量相匹配。您可能需要调整中断或颜色
  • 我认为您当前的设置需要 9 种颜色,所以再增加一种
  • 它真的对我有用,感谢@dieghernan 的详细回答
【解决方案2】:

这个问题非常有用。通过继续这个,我有一个小问题。我怎样才能在一个连续的水平上制作传奇。我附上了相同的复制代码(从上面)。有人可以编辑它以连续方式而不是离散方式制作传奇吗?

     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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-22
    • 2016-07-26
    相关资源
    最近更新 更多