ggplotly tooltip for geom_text and geom_segment; does not work on Mac

511 views Asked by At

I am working on revising a CRAN package. I have one function call that seems to only cause errors on Mac. It caused an error on r-release-osx-x86_64-mavericks on CRAN. I ran it on El Capitan and Mavericks and it also produced the same error on both of these Mac versions. All other systems checked by CRAN (solaris, linux, windows) did not cause an error.

I created a MWE of the function call as follows (sorry it is long to create the data). Basically, it starts with creating a ggplot2 figure using 3 data frames (edgeDF - all non-path edges, nodeDF - all non-path nodes, pathDF - edges and nodes for path of interest):

set.seed(1)
label=c()
for (i in 1:10){
  label[i] = paste(sample(letters,sample(3:10, 1),replace=TRUE),collapse='')  
}
nodeDF = data.frame(label = label, x = runif(10,0,10), y = runif(10,0,10))

edgeDF = data.frame()
edgeDF = rbind(edgeDF, c(x=nodeDF[1,]$x, xend=nodeDF[1,]$y, y=nodeDF[2,]$x, yend=nodeDF[2,]$y))
edgeDF = rbind(edgeDF, c(x=nodeDF[5,]$x, xend=nodeDF[5,]$y, y=nodeDF[6,]$x, yend=nodeDF[6,]$y))
colnames(edgeDF) = c("x","y","xend","yend")

r1=runif(1,0,10)
r2=runif(1,0,10)
r3=runif(1,0,10)
r4=runif(1,0,10)

label=c()
for (i in 1:3){
  label[i] = paste(sample(letters,sample(3:10, 1),replace=TRUE),collapse='')  
}
xend = c(r1, r2, runif(1,0,10))
xstart = c(runif(1,0,10), r1, r2)
yend = c(r3, r4, runif(1,0,10))
ystart = c(runif(1,0,10), r3, r4)

pathDF = data.frame(label=label,xstart=xstart,ystart=ystart,xend=xend,yend=yend,x=xstart,y=ystart)

edgeCol="gray84"
pathEdgeCol="seagreen"
nodeCol="black"

plotTotalImage = ggplot2::ggplot(data = nodeDF, ggplot2::aes(x = x, y = y)) +
  ggplot2::geom_segment(data = edgeDF, ggplot2::aes(x=x, y=y, xend=xend, yend=yend), colour = edgeCol) +
  ggplot2::geom_segment(data = pathDF, ggplot2::aes(x=xstart, y=ystart, xend=xend, yend=yend), colour = pathEdgeCol) +
  ggplot2::geom_text(data = nodeDF, ggplot2::aes(x = x, y = y, label = label), colour = nodeCol)

plotTotalImage = plotTotalImage + ggplot2::geom_text(data = pathDF,ggplot2::aes(x = x, y = y, label = label), fontface= "bold")

I call geom_segment to create the edges (gray ones are off path, green ones are on path). I call geom_text to create node labels (non-bold ones are off path, bold ones are on path). This successfully creates a static image.

At this point, I want to use plotly to add interactivity to the plot. I do not want information to hover for segments; I only want information to hover for node labels. When the mouse hovers over a node label, I want the x value and the label name to be displayed.

The following code works as intended on Windows:

animatePlotTotalImage <- plotly::plotly_build(plotly::ggplotly(plotTotalImage, tooltip = c("x", "label")))
animatePlotTotalImage$data[[1]]$hoverinfo <- "none"
animatePlotTotalImage$data[[2]]$hoverinfo <- "none"
animatePlotTotalImage$data[[3]]$hoverinfo <- c("x+text")
animatePlotTotalImage$data[[4]]$hoverinfo <- c("x+text")
animatePlotTotalImage 

However, on Mac, it results in the following Error:

Error in `*tmp*`[[2]] : subscript out of bounds

I am unsure how to go about solving this problem. Any ideas or suggestions would be greatly appreciated!

1

There are 1 answers

1
MLavoie On BEST ANSWER

I might be wrong but I think you have $x missing from your statements:

This works for me (mac Sierra):

animatePlotTotalImage <- plotly::plotly_build(plotly::ggplotly(plotTotalImage, tooltip = c("x", "label")))
animatePlotTotalImage$x$data[[1]]$hoverinfo <- "none"
animatePlotTotalImage$x$data[[2]]$hoverinfo <- "none"
animatePlotTotalImage$x$data[[3]]$hoverinfo <- c("x+text")
animatePlotTotalImage$x$data[[4]]$hoverinfo <- c("x+text")