RHT
RHT

Reputation: 21

Change krig plot with a continuous scale to discrete

I'm trying to solve a krigging/ggplot issue. I currently have the plot showing a continuous scale using: continuous scale

    Cu_DTPA_NL.kriged %>%
      as.data.frame() %>%
      ggplot(aes(x = x, y = y)) +
      geom_raster(aes(fill = var1.pred), interpolate = TRUE) +
      coord_equal() +
      scale_fill_viridis(option = "inferno") +
      scale_x_continuous(labels = comma) +
      scale_y_continuous(labels = comma) +
      theme_bw()

But I want to change is colour ramp to discrete, selecting my own colours, scale, adn include contours but I am unable to get it to work, see example image and code below: Example of output

  Zn_DTPA_NL.kriged <- krige(Zn ~ 1, DTPA_North, North_krige_grid, model = lzn_DTPA_NL.fit)
  Zn_DTPA_NL.kriged %>%
    as.data.frame() %>%
    ggplot(aes(x = x, y = y)) +
    geom_raster(aes(fill = var1.pred), interpolate = TRUE) +
    coord_equal() +
    scale_colour_manual(
      breaks = c("550", "650", "750", "850"),
      labels = c("550", "650", "750", "850"),
      values = c(
        "#0000FF", "#33CCFF",
        "#99FF99", "#FFCC33", "#CC0000"
      )
    ) +
    scale_fill_manual(
      breaks = c("550", "650", "750", "850"),
      labels = c("550", "650", "750", "850"),
      values = c(
        "#0000FF", "#33CCFF",
        "#99FF99", "#FFCC33", "#CC0000"
      )
    ) +
    scale_x_continuous(labels = comma) +
    scale_y_continuous(labels = comma) +
    theme_bw()

Any advice will be great!

Upvotes: 0

Views: 211

Answers (2)

Elio Campitelli
Elio Campitelli

Reputation: 1476

Use geom_contour_fill(). This would get you 80% there.

Cu_DTPA_NL.kriged %>%
  as.data.frame() %>%
  ggplot(aes(x = x, y = y)) +
  geom_contour_filled(aes(z = var1.pred)) +
  coord_equal() +
  scale_x_continuous(labels = comma) +
  scale_y_continuous(labels = comma) +
  theme_bw()

Upvotes: 0

RHT
RHT

Reputation: 21

I got the answers from the Ecology in R group on Facebook.

The first option is manually binning using:

dat <- data.frame(Zn_DTPA_NL.kriged)
# make a new column with discrete breaks
dat$brks <- cut(dat$var1.pred,
breaks=c(0, 550, 650, 750, 850, Inf),
labels = c("0-550", "550-650", "650-750", "750-850", ">850))

The second is auto binning using:

scale_fill_viridis_b()

Upvotes: 0

Related Questions