【问题标题】:Detailed Heatmap with plotly带有 plotly 的详细热图
【发布时间】:2019-07-18 19:40:20
【问题描述】:

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

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

结果:

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

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

有什么建议/提示吗?我正在尝试做的事情有可能吗?

【问题讨论】:

  • 你可以试试input %&gt;% ggplot2::ggplot(aes(text=Study)) ......
  • 我做了,但这不起作用,因为它会干扰 geom_count 命令。结果是每个点的 size=1。
  • 我错过了这个问题。每点有几项研究。在我的回答中,我手动构建了带有计数的表格以及用于研究的列,然后我使用 geom_pointsize 美学。

标签: r ggplot2 heatmap ggplotly


【解决方案1】:

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)

【讨论】:

  • 非常感谢您的帮助,非常感谢 :)
猜你喜欢
  • 2012-08-10
  • 2020-08-30
  • 1970-01-01
  • 1970-01-01
  • 2022-09-25
  • 2012-07-21
  • 1970-01-01
  • 1970-01-01
  • 2021-06-08
相关资源
最近更新 更多