Search code examples
rggplot2heatmapggplotly

Detailed Heatmap with plotly


I tried to create a heatmap with the following data:

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

My code:

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())

Result: enter image description here

I was wondering if I could add a label/text to the points, so that if I hover over the points, I could not only see the number of studies but also the studies themselves. I tried the following, but it didnt really work:

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

Any advice/tips? Is the thing even possible im trying to do?


Solution

  • This is painful with geom_count. Here is a way using 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)