简体   繁体   English

如何在ggplotly中调整hover?

[英]How to adjust hover in ggplotly?

I have data with 2 categorical and 1 numerical column.我有 2 个分类列和 1 个数值列的数据。 I built marikemo chart in ggplot , and in order to make it interactive, I used ggplotly() .我在ggplot中构建了 marikemo 图表,为了使其具有交互性,我使用ggplotly() However, when I move a cursor, hover shows the same information twice:但是,当我移动 cursor 时,hover 两次显示相同的信息:

# create a dataset
specie <- c(rep("sorgho" , 3) , rep("poacee" , 3) , rep("banana" , 3) , rep("triticum" , 3) )
condition <- rep(c("normal" , "stress" , "Nitrogen") , 4)
value <- abs(rnorm(12 , 0 , 15))
data <- data.frame(specie,condition,value)


library(tidyverse)
library(plotly)

plt <- data %>% 
  group_by(specie) %>%
  mutate(value = value / sum(value)) %>%
  ggplot(aes(fill=condition, y=value, x=specie)) + 
  geom_col(position="fill", width = 1, color = "white") +
  geom_text(aes(label = scales::percent(value, accuracy = 0.1)), 
            position = position_fill(vjust = 0.50),
            color = "white") +
  scale_y_continuous(labels = scales::percent) +
  scale_fill_brewer(palette = "Set1")

ggplotly(plt)

在此处输入图像描述

Sometimes what comes from ggplotly is odd.有时来自ggplotly的东西很奇怪。 The tip, sure.小费,当然。 The black background?黑色背景? Where did it get that from?它是从哪里得到的? Either way, I used lapply to determine which traces had either text or hovertext that contained the word "scale".无论哪种方式,我都使用lapply来确定哪些轨迹的texthovertext文本包含“刻度”一词。 I chose scale because it was string that only appeared in the tooltip that needed to go away.我选择 scale 是因为它是只出现在工具提示中的字符串,需要 go 离开。 Then I used any as in "is anything true?"然后我在“有什么事是真的吗?”中使用了any and grepl to get a true or false response for matches to the string.grepl以获得对字符串匹配的真或假响应。

plt2 <- ggplotly(plt)              # set to object to modify
invisible(lapply(1:length(plt2$x$data),
                 function(j) {
                   txt <- plt2$x$data[[j]]$text
                   htxt <- plt2$x$data[[j]]$hovertext
                   if(any(grepl("scale", txt)) | # any matches per trace
                      any(grepl("scale", htxt))) {
                     plt2$x$data[[j]]$hovertext <<- ""
                     plt2$x$data[[j]]$hoverinfo <<- "none" 
                   }
                 }))
plt2

The extra tip is no longer shown.不再显示额外的提示。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM