gpt4 book ai didi

r - 如何更改 ggplot2 geom_raster 中的插值/平滑

转载 作者:行者123 更新时间:2023-12-04 11:15:31 25 4
gpt4 key购买 nike

是否可以在 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")

enter image description here

我想控制下图中发生的模糊程度:
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")

enter image description here

我知道如何用 stat_density_2d 做到这一点-- see this post - 但我想通过填充计算值而不是计算密度。

最佳答案

AFAIK,除了打开或关闭它之外,您无法操纵网格函数的插值。相反,ggfx 包允许您使用您选择的“sigma”参数模糊特定图层。下面的例子:

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.

创建于 2021-08-29 由 reprex package (v2.0.0)

关于r - 如何更改 ggplot2 geom_raster 中的插值/平滑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50881044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com