I have a bit of code in a function that I hope to use to create a heatmap-like effect around points in ggplot. The [col, row] coordinates are coming from a 2D map mockup done in Excel... admittedly janky but that's what I have to work with right now.
In any case, the below reprex produces the desired effect when passing point values, but I get an error non-numeric argument to binary operator when I attempt to apply to vectors of points stored in a dataframe. Thanks for any insights!
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(tidyr)
library(purrr)
library(tibble)
library(stringr)
get_circle_kernel <- function(col_pts = 10,
row_pts = 10,
radius = 5L,
jitter = 5L) {
# sample the row and column jitter to move the circle center
col_jitter <- sample(-jitter:jitter, size = 1)
row_jitter <- sample(-jitter:jitter, size = 1)
# update col_pts and row_pts to move circle center
col_pts <- col_pts + col_jitter
row_pts <- row_pts + row_jitter
# draw a filled circle of points with alpha for each [row_pts, col_]
# from https://stackoverflow.com/questions/14285358/find-all-integer-coordinates-in-a-given-radius
df_circle <- tibble::tibble(
row = (row_pts - radius):(row_pts + radius),
col = (col_pts - radius):(col_pts + radius)
) %>% tidyr::expand(row, col) %>%
dplyr::mutate(radius_from_center = sqrt((row - row_pts) ^ 2 + (col - col_pts) ^
2)) %>%
dplyr::filter(radius_from_center <= radius) %>%
dplyr::mutate(alpha = (max(radius_from_center) ^ 2 - radius_from_center ^
2) / max(radius_from_center) ^ 2)
return(df_circle)
}
# Plots for testing & noodling --------------------------------------------
df_1 <- get_circle_kernel()
ggplot2::ggplot(df_1, ggplot2::aes(x = col, y = row, alpha = alpha)) +
ggplot2::geom_raster(interpolate = FALSE, fill = "red") +
ggplot2::scale_x_continuous(limits = c(0, 20)) +
ggplot2::scale_y_continuous(limits = c(0, 20))

# Example applying to df --------------------------------------------------
df_2 <- tibble::tibble(col = c(10, 30, 50),
row = c(10, 30, 50),
radius_from_center = rep(0, 3),
alpha = rep(1, 3)) %>%
purrr::map_df(~get_circle_kernel(col_pts = col, row_pts = row))
#> Error in col_pts + col_jitter: non-numeric argument to binary operator
Created on 2021-02-19 by the reprex package (v1.0.0)