带有 plotly 的详细热图

Detailed Heatmap with plotly

我尝试使用以下数据创建热图:

Study   Intervention                Outcome
Study_1 Monetary_Incentive          Emission_Reduction
Study_2 Govermental_Change          Emission_Reduction
Study_3 Nudges                      Renewable_Energy_Usage
Study_3 Market_based_intervention   Renewable_Energy_Usage
Study_4 Market_based_intervention   Emission_Reduction
Study_5 Monetary_Incentive          Renewable_Energy_Usage
Study_6 Nudges                      Emission_Reduction
Study_6 Govermental_Change          Transport
Study_7 Market_based_intervention   Renewable_Energy_Usage
Study_8 Monetary_Incentive          Renewable_Energy_Usage
Study_9 Market_based_intervention   Emission_Reduction
Study_10 Market_based_intervention  Emission_Reduction
Study_10 Monetary_Incentive         Renewable_Energy_Usage
Study_11 Market_based_intervention  Transport
Study_12 Govermental_Change         Transport
Study_13 Monetary_Incentive         Emission_Reduction
Study_14 Nudges                     Transport

我的代码:

input <- read.csv2("Test_dataset.csv")

axis_txt_lim = 60
library(dplyr)
library(ggplot2)


test <-  input%>% ggplot2::ggplot() +
  ggplot2::geom_count(aes(x = Outcome, y = Intervention),colour = "light 
  blue", fill ="light blue") +
  ggplot2::theme_bw() +
  ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 45, hjust = 1),
             panel.grid = element_blank(), 
             text = ggplot2::element_text(size = 14), 
             axis.title = ggplot2::element_text(size = 16), 
             title = ggplot2::element_text(size = 18),
             legend.position="none") + 
ggplot2::xlab(paste0(colnames(data)[2])) +
ggplot2::ylab(paste0(colnames(data)[3])) +
ggplot2::scale_x_discrete(labels = function(x) substr(x, 1, axis_txt_lim))+
ggplot2::scale_y_discrete(labels = function(x) substr(x, 1, axis_txt_lim)) + 
ggplot2::ggtitle("Evidence Gap Map", subtitle = paste(colnames(data)[3], 
"by", colnames(data)[2]))
test
plotly::ggplotly(test = ggplot2::last_plot())

结果:

我想知道我是否可以在这些点上添加一个 label/text,这样如果我将鼠标悬停在这些点上,我不仅可以看到研究的数量,还可以看到研究本身。 我尝试了以下方法,但它并没有真正起作用:

test + geom_text(aes(x = Outcome, y = Intervention, label = Study)) 
plotly::ggplotly(test = ggplot2::last_plot())

有advice/tips吗?我想做的事情有可能吗?

geom_count 这很痛苦。这是一种使用 geom_point:

的方法
library(data.table)
dt0 <- as.data.table(input)
dt <- 
  dt0[, .(studies = paste0(Study, collapse=","), n = .N), by=c("Outcome","Intervention")]
dt[, n:=as.factor(n)]
gg <- ggplot(dt) + 
  geom_point(aes(x = Outcome, y = Intervention, size = n, text = studies)) + 
  scale_size_manual(name = "Nr studies", values = as.numeric(levels(dt$n))) # better legend

ggplotly(gg)