ggplotly tooltip is showing data twice

I have 2 datasets included in one chart using ggplot. I am using ggplotly to create a tooltip but the information in the tooltips for the 2 points is showing twice. The following code is a little lengthy but will recreate the chart:

library(ggplot2)
library(plotly)

AreaName <- c("A", "B", "C", "A", "B", "C")
Timeperiod <- c("2018", "2018", "2018", "2019", "2019", "2019")
Value <- c(11.5, 39.3, 9.4, 14.2, 40.7, 19.1)
df <- data.frame(cbind(AreaName, Timeperiod, Value), stringsAsFactors = F)
df$Value <- as.numeric(df$Value)

AreaName <- c("A", "A")
Timeperiod <- c("2019", "2020")
qtr <- c("Q1-Q2", "Q1-Q2")
Value <- c(15.6, 10.2)
df2 <- data.frame(cbind(Timeperiod, qtr, AreaName, Value), stringsAsFactors = F)
df2$Value <- as.numeric(df2$Value)

ggp <- ggplotly(ggplot(data = df, aes(x=Timeperiod, y=Value, group = AreaName, colour = AreaName, text = paste("Area name: ", AreaName, "<br>Time period: ", Timeperiod, "<br>Rate: ", round(Value,1), "per 100,000"))) +
  geom_line() +
  geom_point() +
  geom_point(data = df2, aes(shape = c(paste(AreaName, qtr, Timeperiod)),text = paste("Area name: ", AreaName, "<br>Quarter: ", qtr, "<br>Time period: ", Timeperiod, "<br>Rate: ", round(Value,1), "per 100,000"))) +
  scale_shape_manual(values = c(18, 17)) +
  theme(axis.text.x = element_text(vjust = 0.5), axis.title.x = element_blank()) +
  labs(y = "Crude rate per 100,000 persons all ages", colour = "Area", shape = "") +
  guides(shape = guide_legend(order = 2),colour = guide_legend(order = 1)) +
  expand_limits(y=0), tooltip = "text")

ggpNames <- unique(df$AreaName)
legs <- paste(df2$AreaName, df2$qtr, df2$Timeperiod)
ggpNames <- c(ggpNames,legs)

for (i in 1:length(ggp$x$data)) { # this goes over all places where legend values are stored
    n1 <- ggp$x$data[[i]]$name # and this is how the value is stored in plotly
    n2 <- " "
    for (j in 1:length(ggpNames)) {
        if (grepl(x = n1, pattern = ggpNames[j])) {n2 = ggpNames[j]} # if the plotly legend name contains the original value, replace it with the original value
    }
    ggp$x$data[[i]]$name <- n2 # now is the time for actual replacement
    if (n2 == " ") {ggp$x$data[[i]]$showlegend = FALSE}  # sometimes plotly adds to the legend values that we don't want, this is how to get rid of them, too
}

ggp %>% config(displaylogo = FALSE, modeBarButtonsToRemove = list("autoScale2d", "resetScale2d","select2d", "lasso2d", "zoomIn2d", "zoomOut2d", "toggleSpikelines", "zoom2d", "pan2d"))

ggp

Does anyone have an elegant solution to this? Thanks

I can't run your code as it errors:

Error in mm2pixels(convert(u, "mm")) : Unit must be in millimeters
In addition: Warning message:
Ignoring unknown aesthetics: text 

I do think ggplotly is fine for quickly plotlifying straightforward ggplot charts without much effort, but it seems to me when you find yourself hacking on it hard, I really think you will get more value at the end of the day from using plotly directly with plotly syntax etc.

Thanks nirgrahamuk,
I don't know why it's showing an error. I will have to use plotly more to get to that point but have to stick with what I know at the moment.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.