Is it possible to change the level of interpolation (e.g. smoothing, blur) in geom_raster
?
library(tidyverse)
mtcars %>%
group_by(carb, hp = cut(mtcars$hp, 3, labels = c("low", "med", "hi"))) %>%
summarise(mean_mpg = mean(mpg)) %>%
ggplot(aes(carb, hp)) +
geom_raster(aes(fill = mean_mpg), interpolate = FALSE) +
scale_fill_viridis_c(option = "inferno")
I'd like to have control over how much blurring takes place in the following graph:
mtcars %>%
group_by(carb, hp = cut(mtcars$hp, 3, labels = c("low", "med", "hi"))) %>%
summarise(mean_mpg = mean(mpg)) %>%
ggplot(aes(carb, hp)) +
geom_raster(aes(fill = mean_mpg), interpolate = TRUE) +
scale_fill_viridis_c(option = "inferno")
I know how to do this with stat_density_2d
-- see this post -- but I would like to pass fill a calculated value and not to calculate a density.
AFAIK, you can't manipulate the interpolation of the grid function other than just turning it on or off. Instead, the ggfx package allows you to blur a specific layer with a 'sigma' parameter of your choosing. Example below:
library(tidyverse)
library(ggfx)
mtcars %>%
group_by(carb, hp = cut(mtcars$hp, 3, labels = c("low", "med", "hi"))) %>%
summarise(mean_mpg = mean(mpg)) %>%
ggplot(aes(carb, hp)) +
with_blur(
geom_raster(aes(fill = mean_mpg), interpolate = FALSE),
sigma = 10
) +
scale_fill_viridis_c(option = "inferno")
#> `summarise()` has grouped output by 'carb'. You can override using the `.groups` argument.
#> Warning: Raster pixels are placed at uneven horizontal intervals and will be
#> shifted. Consider using geom_tile() instead.
Created on 2021-08-29 by the reprex package (v2.0.0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With