r - ggplot : transperancy of histogram as function of stat(count)

标签 r ggplot2

我正在尝试以这种方式制作缩放直方图,每个“列”(bin?)的透明度取决于给定 x 范围内的观察数量。这是我的代码:

set.seed(1)
test = data.frame(x = rnorm(200, mean = 0, sd = 10),
                  y = as.factor(sample(c(0,1), replace=TRUE, size=100)))
threshold = 20 
ggplot(test,
       aes(x = x))+
  geom_histogram(aes(fill = y, alpha = stat(count) > threshold),
                 position = "fill", bins = 10)

基本上我想制作看起来像这样的图:

enter image description here

但是我的代码生成的图表基于分组后的计数应用透明度,最终以这样的挂列结束:

enter image description here

对于这个例子,为了模拟一个“正确的”图,我只是调整了阈值,但我需要 alpha 来考虑给定“列”(bin) 中两组的计数总和。

更新: 我还希望它以这样一种方式处理分面图,即每个分面中的突出显示区域独立于其他分面。 @Stefan 提出的方法非常适合单个情节,但在多面情节中突出显示所有方面的同一区域。

library(ggplot2)

set.seed(1)
test = data.frame(x = rnorm(1000, mean = 0, sd = 10),
                  y = as.factor(sample(c(0,1), replace=TRUE, size=1000)),
                  n = as.factor(sample(c(0,1,2), replace=TRUE, size=1000)),
                  m = as.factor(sample(c(0,1,3,4), replace=TRUE, size=1000)))
f = function(..count.., ..x..) tapply(..count.., factor(..x..), sum)[factor(..x..)]
threshold = 10 
ggplot(test,
       aes(x = x))+
  geom_histogram(aes(fill = y, alpha = f(..count.., ..x..) > threshold),
                 position = "fill", bins = 10)+
  facet_grid(rows = vars(n),
             cols = vars(m))

enter image description here

最佳答案

这可以这样实现:

  1. 由于 stat_count 计算的 count 是分组后的 obs 数量,我们必须手动汇总各组的 count 以获得总数count 每个 bin。
  2. 为了汇总每个 bin 的计数,我使用 tapply,其中我使用 .. 符号来获取由 stat_count 计算的变量.
  3. 作为分组变量,我使用了计算变量 ..x..,据我所知,它没有记录。基本上 ..x.. 默认包含 bin 的中点,因此可以用作 bin 的标识符。但是,由于这些是连续值,我们已将它们转换为一个因子。

最后,为了使代码更具可读性,我使用了一个辅助函数来计算聚合计数。此外,我将 threshold 值加倍到 20。

library(ggplot2)

set.seed(1)
test <- data.frame(
  x = rnorm(200, mean = 0, sd = 10),
  y = as.factor(sample(c(0, 1), replace = TRUE, size = 100))
)
threshold <- 20

f <- function(..count.., ..x..) tapply(..count.., factor(..x..), sum)[factor(..x..)]
p <- ggplot(
  test,
  aes(x = x)
) +
  geom_histogram(aes(fill = y, alpha = f(..count.., ..x..) > threshold),
    position = "fill", bins = 10
  )
p

enter image description here

EDIT 为了允许分面,我们必须将 ..PANEL.. 标识符作为附加参数传递给函数。我不再使用 tapply,而是使用 dplyr::group_bydplyr::add_count 来计算每个 bin 和 facet 面板的总数:

library(ggplot2)
library(dplyr)

set.seed(1)
test <- data.frame(
  x = rnorm(200, mean = 0, sd = 10),
  y = as.factor(sample(c(0, 1), replace = TRUE, size = 100)),
  type = rep(c("A", "B"), each = 100)
)
threshold <- 20

f <- function(count, x, PANEL) {
  data.frame(count, x, PANEL) %>% 
    add_count(x, PANEL, wt = count) %>% 
    pull(n)
}
p <- ggplot(
  test,
  aes(x = x)
) +
  geom_histogram(aes(fill = y, alpha = f(..count.., ..x.., ..PANEL..) > threshold),
                 position = "fill", bins = 10
  ) +
  facet_wrap(~type)
p
#> Warning: Using alpha for a discrete variable is not advised.
#> Warning: Removed 2 rows containing missing values (geom_bar).

关于r - ggplot : transperancy of histogram as function of stat(count),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64382175/

相关文章:

r - "A"= "a"、 "B"= "b"等形式的表达式如何从 LETTERS 和字母自动构建?

r - 在R中: help using rle() function in dataframe

r - ggplot2 中 x 轴上的额外空白

r - ggplot2 geom_bar位置失败

r - 在ggplot2中绘制运行平均值

r - 可以在格子和 ggplot2 图中使用多边形()或等效项吗?

r - 过滤所有列中包含特定字符串的行(使用 dplyr)

r - 将引号添加到R中的向量

r - 获取 Shiny 的 slickR 幻灯片的当前图像名称

r - 为什么在 R ggplot2::facet_grid() 中传递字符串适用于行但不适用于列?