I have data with 2 categorical and 1 numerical column. I built marikemo chart in ggplot
, and in order to make it interactive, I used ggplotly()
.
However, when I move a cursor, hover shows the same information twice:
# 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)
CodePudding user response:
Sometimes what comes from ggplotly
is odd. 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". I chose scale because it was string that only appeared in the tooltip that needed to go away. Then I used any
as in "is anything true?" and grepl
to get a true or false response for matches to the string.
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.