Skip to content

Instantly share code, notes, and snippets.

@alexpghayes
Created October 5, 2022 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexpghayes/c596ca66abe4fb9c83bb0e9c2b342d36 to your computer and use it in GitHub Desktop.
Save alexpghayes/c596ca66abe4fb9c83bb0e9c2b342d36 to your computer and use it in GitHub Desktop.
library(tidyverse)
data <- read_csv("data.csv")
X <- as.matrix(data) # i presume this is the starting state of the data
plot_at_threshold <- function(mat, threshold) {
colnames(mat) <- 1:NCOL(mat)
rownames(mat) <- 1:NROW(mat)
tbl <- as_tibble(mat, rownames = "row")
threshold_to <- min(mat)
long <- tbl %>%
pivot_longer(
-row,
names_to = "col"
) %>%
mutate_at(vars(row, col), as.integer) %>%
mutate(
value = if_else(value > threshold, value, threshold_to),
thresholded = (value == threshold_to),
xmin = row,
xmax = row + 1,
ymin = col,
ymax = col + 1
)
not_thresholded <- long %>%
filter(!thresholded)
long %>%
ggplot() +
aes(
xmin = xmin,
xmax = xmax,
ymin = ymin,
ymax = ymax,
fill = value
) +
geom_rect() +
geom_point(
data = not_thresholded,
aes(x = xmin, y = ymin),
color = "white"
) +
geom_point(
data = not_thresholded,
aes(x = xmin, y = ymax),
color = "white"
) +
geom_point(
data = not_thresholded,
aes(x = xmax, y = ymin),
color = "white"
) +
geom_point(
data = not_thresholded,
aes(x = xmax, y = ymax),
color = "white"
) +
geom_segment(
data = not_thresholded,
aes(x = xmin, xend = xmax, y = ymax, yend = ymax),
color = "white"
) +
geom_segment(
data = not_thresholded,
aes(x = xmin, xend = xmax, y = ymin, yend = ymin),
color = "white"
) +
geom_segment(
data = not_thresholded,
aes(x = xmin, xend = xmin, y = ymin, yend = ymax),
color = "white"
) +
geom_segment(
data = not_thresholded,
aes(x = xmax, xend = xmax, y = ymin, yend = ymax),
color = "white"
) +
geom_segment(
data = not_thresholded,
aes(x = xmin, xend = xmax, y = ymin, yend = ymax),
color = "white"
) +
scale_fill_viridis_c(limits = range(X)) + # play with colors here
scale_x_continuous(
breaks = 0:NROW(mat) + 1,
labels = as.character(0:NROW(mat) / NROW(mat))
) +
scale_y_continuous(
breaks = 0:NCOL(mat) + 1,
labels = as.character(0:NCOL(mat) / NCOL(mat))
) +
theme_minimal() +
labs(
x = "",
y = "",
fill = "",
title = glue::glue("set=(Inf, {threshold}]")
)
}
plot_at_threshold(X, 1500)
plot_at_threshold(X, 1400)
plot_at_threshold(X, 1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment