[英]How to adjust hover in ggplotly?
我有 2 个分类列和 1 个数值列的数据。 我在ggplot
中构建了 marikemo 图表,为了使其具有交互性,我使用ggplotly()
。 但是,当我移动 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)
有时来自ggplotly
的东西很奇怪。 小费,当然。 黑色背景? 它是从哪里得到的? 无论哪种方式,我都使用lapply
来确定哪些轨迹的text
或hovertext
文本包含“刻度”一词。 我选择 scale 是因为它是只出现在工具提示中的字符串,需要 go 离开。 然后我在“有什么事是真的吗?”中使用了any
。 和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
不再显示额外的提示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.