Hi, I'm trying to solve a krigging/ggplot issue.

1 year ago 10 Replies
RT
Roger Tang
3 years ago

Hi, I'm trying to solve a krigging/ggplot issue. I currently have the plot showing a continuous scale using:
Cu_DTPA_NL.kriged <- krige(Cu ~ 1, DTPA_North, North_krige_grid, model = lcu_DTPA_NL.fit)
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:

  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!

9 Likes

Replies

Ryan McCarley 3 years ago

I think scale_fill_manual should be working for your geom_raster, but I don't see a color aesthetic defined anywhere. If you delete the color_fill_manual section does that get you closer to what you want?

0 Likes
Roger Tang (3 years ago)

Ryan McCarley , sorry fixed it:
dat <- data.frame(Zn_DTPA_NL.kriged) 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"))

  Zn_DTPA_NL.kriged %>%
    as.data.frame() %>%
    ggplot(aes(x = x, y = y)) +
    geom_raster(aes(fill = dat$brks), interpolate = TRUE) +
    coord_equal() +
    scale_fill_manual(
      values = c(
        "#0000FF", "#33CCFF",
        "#99FF99", "#FFCC33", "#CC0000"
      )
    ) +
    scale_x_continuous(labels = comma) +
    scale_y_continuous(labels = comma) +
    theme_bw()

Russell Gray 3 years ago

use the cut() function to create custom numeric divisons and then feed that to the plot instead

1 Like
Russell Gray 3 years ago

for example: 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))

2 Likes
Russell Gray 3 years ago

Then use that new column as your fill

2 Likes
Barrett Wolfe 3 years ago

yeah cut it or you might use scale_fill_viridis_b() and have it do the binning for you

1 Like
Roger Tang 3 years ago

Thank you all for your help!

0 Likes
Roger Tang 3 years ago

Does anyone know how to add the contour lines? I tried geom_contour()

0 Likes
Barrett Wolfe (3 years ago)

Roger Tang should be able to use stat_contour() with z mapped to whatever the fill colour variable is

Roger Tang (3 years ago)

Barnabas Daru Thank you!