79681256

Date: 2025-06-26 23:17:46
Score: 0.5
Natty:
Report link

This turned out to be happening because the first trace had no entries for y == "one", and so plotly considered the entire top row to contain gaps. My hacky solution for this was to add NA's for that whole row, and it seems to have fixed the issue:

library(tidyverse)
library(plotly)

# generate some sample data (somewhat clunkily)
# x and y column are all unique combinations of the words "one" through "four"
# v1 and v2 columns contain random data with different ranges
f <- combn(c("one","two","three","four"),2) %>%
  t() %>%
  as.data.frame() %>%
  rename(x=1,y=2) %>%
  mutate(v1 = rnorm(n(),5), v2 = rnorm(n(), 200, sd=55)) %>%
  arrange(x,y) %>%
  mutate(
    x = factor(x,c("one","two","three","four")),
    y = factor(y,c("four","three","two","one"))
  )

lwr_scale <- list(list(0,"black"),list(1,"red"))
upr_scale <- list(list(0,"white"),list(1,"green"))


# get factor level for the top row
last_l <- last(levels(f$y))
top_row <- f %>%
  # get entries for x == last_l
  filter(x == last_l) %>%
  # swap x and y
  mutate(temp=y,y=x,x=temp) %>%
  select(-temp) %>%
  # set numeric columns to NA
  mutate(across(where(is.numeric),~NA))

# add dummy rows back to original dataset
f <- bind_rows(f,top_row)

f %>%
  plot_ly(hoverongaps=FALSE) %>% 
  add_trace(
    type = "heatmap",
    x = ~x,
    y = ~y,
    z = ~v1,
    colorscale = lwr_scale
  ) %>%
  add_trace(
    type="heatmap",
    x = ~y,
    y = ~x,
    colorscale = upr_scale,
    z = ~v2
  )

et voila:

enter image description here

Reasons:
  • Probably link only (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Luther Blissett