0

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!

RHT
  • 21
  • 3
  • 1
    Maybe try binning the values first and create a factor column with that data. Then in ggplot you could use a scale_fill_manual() on your new factor column. – Josh Nov 20 '20 at 01:48

2 Answers2

0

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()
RHT
  • 21
  • 3
0

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()
Elio Campitelli
  • 1,408
  • 1
  • 10
  • 20