ggplot2 中热图的衰减函数

decay function for heatmap in ggplot2

有一个主要是数学问题,其中包含空间成分。我有一个用于心理学实验的综合任务环境的 2D 自上而下地图,我目前在电子表格中表示。我们希望使用热图向参与者展示此环境中的某些位置,并使用某些衰减函数添加一些关于特定兴趣点的模糊性。创建以这样的内容开头的数据框或矩阵的简单方法是什么(5 是感兴趣的位置):

0,0,0,0,0,0
0,0,0,0,0,0
0,0,5,0,0,0
0,0,0,0,0,0
0,0,0,0,0,0
0,0,0,0,0,0

并将其变成类似这样的东西,我可以将其传递给 ggplot2 geom_bin2d() 或类似的东西?

1,1,1,1,1,0
1,3,3,3,1,0
1,3,5,3,1,0
1,3,3,3,1,0
1,1,1,1,1,0
0,0,0,0,0,0

作为后续,有没有简单的方法可以在程序上 jitter/offset 创建一个不完全在 'true' 位置上方的模式?

0,1,1,1,1,1
0,1,3,3,3,1
0,1,3,5,3,1
0,1,3,3,3,1
0,1,1,1,1,1
0,0,0,0,0,0

感谢您的帮助!

了解如何创建我在上面询问的衰减内核。下面的示例函数生成一个圆形内核,但如果你想要一个方形内核,你需要做的就是注释掉下面的 dplyr::filter(radius_from_center... 调用。

您首先需要将矩阵转化为成对向量或具有 x_ptsy_pts 的数据框。由于我使用 tidyxl this step was done for me already. Special thanks also to this SO post 从 2D 地图中提取数据以提供原则上切割圆形图案的解决方案。

    get_circle_kernel <- function(
    x_pts = 0,
    y_pts = 0,
    radius = 5L,
    jitter = 5L){
   if(is.null(x_pts)|is.null(y_pts)){
    stop("Specify a dataframe with `row` and `col` variables.")
  }

  # sample the row and column jitter
  x_jitter <- sample(-jitter:jitter, size = 1)
  y_jitter <- sample(-jitter:jitter, size = 1)

  # update col_pts and row_pts so they reflect jitter
  x_pts <- x_pts + x_jitter
  y_pts <- row_pts + row_jitter

  # draw a filled circle of points with alpha for each [row_pts, col_pts]
  df_circle <-
    tibble::tibble(
    sheet = sheet_pts,
    y = (y_pts - radius):(y_pts + radius),
    x = (x_pts - radius):(x_pts + radius),
    character = character_pts,
    bgColor = bgColor_pts,
    alpha = alpha_pts
  ) %>%
    # df %>%
    tidyr::expand(x, y, tidyr::nesting(sheet, character, bgColor)
                  ) %>%
    dplyr::mutate(radius_from_center = sqrt((x - x_pts) ^ 2 + (y - y_pts) ^
                                              2)) %>%
    dplyr::filter(radius_from_center <= radius) %>%
    dplyr::mutate(alpha = (max(radius_from_center) - radius_from_center) / max(radius_from_center))

  return(df_circle)
  }