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!
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?
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()
use the cut() function to create custom numeric divisons and then feed that to the plot instead
for example: 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))
yeah cut it or you might use scale_fill_viridis_b() and have it do the binning for you
Does anyone know how to add the contour lines? I tried geom_contour()
Roger Tang should be able to use stat_contour() with z mapped to whatever the fill colour variable is
Barnabas Daru Thank you!