如何在ggplot中应用中断?

How to apply break in ggplot?

我有一个范围从 0 到 500 的栅格数据。我已经在我的数据上应用了百分位数,我的代码如下所示。我想在我的代码上应用中断,因为有时我只需要 200-500 的值,所以没有序列选项和中断我不能这样做。

     **breaks = seq (0, 500, 50), limits = c(0, 500))+** # But I don't get where can i apply this in my code

我想在每 50 分钟后休息一次,例如 0-50、50-100 等等....

      r <- raster("D:/research.tif")
      centile90 <- quantile(r, 0.95)
      df <- as.data.frame(as(r, "SpatialPixelsDataFrame"))
      colnames(df) <- c("value", "x", "y")


      ggplot(df, aes(x, y, z = value)) +
      geom_contour_filled(bins = 8) +
      geom_contour(breaks = centile90, colour = "orange",
           size = 0.5) +
      scale_fill_manual(values = hcl.colors(8, "YlGnBu", rev = TRUE)) +
      scale_x_continuous(expand = c(0, 0)) +
      scale_y_continuous(expand = c(0, 0)) +
      theme_classic() +
       theme()

您可以将中断向量传递给 geom_contour_filledbreaks 参数。

让我们首先创建一个值从 0 到 500 的栅格

library(raster)

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

range(r[])
#> [1]   0.00 499.95

df <- as.data.frame(as(r, "SpatialPixelsDataFrame"))
colnames(df) <- c("value", "x", "y")

现在我们这样称呼情节:

centile90 <- quantile(r[], 0.9)

# Define breaks vector
mybreaks <- seq(0, 500, 50)

ggplot(df, aes(x, y, z = value)) +
  geom_contour_filled(breaks = mybreaks) +
  geom_contour(breaks = centile90, colour = "orange",
               size = 0.5) +
  scale_fill_manual(values = hcl.colors(length(mybreaks) - 1, "YlGnBu", rev = TRUE)) +
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0)) +
  theme_classic() +
  theme()