I'm currently in the process of creating a heatmap with plotly. Below is the sample dataset:
library(tidyverse)
library(plotly)
library(hrbrthemes)
set.seed(9999)
df <- data.frame(group.int = rep(c(rep("Prevention", 3), "Diagnosis", rep("Intervention", 2)), 6),
int = rep(c("Prevention 1", "Prevention 2", "Prevention 3", "Diagnosis 1", "Intervention 1", "Intervention 2"), 6),
group.outcome = c(rep("Efficacy", 12), rep("Safety", 18), rep("Cost-effectiveness", 6)),
outcome = c(rep("Efficacy 1", 6), rep("Efficacy 2", 6), rep("Safety 1", 6), rep("Safety 2", 6), rep("Safety 3", 6), rep("Cost-effectiveness 1", 6)),
n = sample(50:250, 36, rep = TRUE)
)
df$group.int <- factor(df$group.int, levels = c("Prevention", "Diagnosis", "Intervention"))
df$group.outcome <- factor(df$group.outcome, levels = c("Efficacy", "Safety", "Cost-effectiveness"))
I want to make a heatmap based on the variable outcome against int, with n as the fill of each heatmap cell. Here is the desired plot:
I tried using ggplotly from the created ggplot:
plotly.df <- ggplot(df,
aes(x = int, y = outcome, fill= n)) +
geom_tile() +
scale_fill_gradient(low="white", high="darkred") +
scale_y_discrete(position = "right") +
facet_grid(group.outcome ~ group.int,
scales = "free", space = "free", switch = "x") +
theme_bw() +
theme(axis.ticks = element_blank(),
legend.position = "left",
strip.placement = "outside",
strip.background = element_blank())
ggplotly(plotly.df)
However, ggplotly seems to ignore space = "free" in facet_grid, so the size of the cells are not proportional:
Is there a way to adjust facet widths with ggplotly?
Thank you very much in advance




