5

I have been trying to create a network graph with the plotly R library. I would like to visualize a custom hoverinfo text that is different from the text label defined for the markers (or nodes in this case).

Basically, I would like to keep the text labels on the markers (nodes) as they are currently but also be able to provide custom text for the hoverinfo. As in I would like the tooltip to display some other character vector different from the marker's label text. For instance, when I hover on the central 'Shigeru Miyamoto' node, I would like to tooltip to display other information that I may collect (eg. type, nationality etc). My understanding from the plotly documentation is that currently you can only display the same text label for both the markers and the hoverinfo tooltip.

Is there a way to accomplish this?

Here's the code I used:

library(plotly)
library(igraph)
library(dplyr)

A <- data.frame(A=rep("Shigeru Miyamoto",10))

B <- data.frame(B=c("Ninendo Company Ltd","Super Mario Bros","Mario", "Gamespot","Time Magazine","Wii","DOnkey Kong Jr","Mario Kart","Japan","Mario Party"))

edgelist <- bind_cols(A,B)

graph <- graph_from_data_frame(edgelist)
L <- layout_nicely(graph)
vs <- V(graph)
es <- as.data.frame(get.edgelist(graph))
Ne <- length(es[1]$V1)
Xn <- L[,1]
Yn <- L[,2]

txt <- list(
        family = "calibri",
        size = 15,
        color = "white",
        opacity = 1
)

size = c(20,rep(5,length(vs)-1))


network <- plot_ly(type = "scatter", x = Xn, y = Yn, mode = "markers+text", 
                   text = names(vs), hoverinfo = "text",marker=list(size=size,color='5BC0DE'), textfont = txt

)


edge_shapes <- list()
for(i in 1:Ne) {
        v0 <- es[i,]$V1
        v1 <- es[i,]$V2

        edge_shape = list(
                type = "line",
                line = list(color = "DF691A", width = 1),
                x0 = Xn[match(v0,names(vs))],
                y0 = Yn[match(v0,names(vs))],
                x1 = Xn[match(v1,names(vs))],
                y1 = Yn[match(v1,names(vs))],
                opacity = 0.3
        )

        edge_shapes[[i]] <- edge_shape}

network <- layout(
        network,
        paper_bgcolor="#4e5d6c",
        plot_bgcolor="#4e5d6c",
        hovermode = "closest",
        title = paste("Miyamoto's Relations",sep = ""),
        titlefont=list(color="white"),
        shapes = edge_shapes,
        xaxis = list(title = "", showgrid = FALSE, showticklabels = FALSE, zeroline = FALSE),
        yaxis = list(title = "", showgrid = FALSE, showticklabels = FALSE, zeroline = FALSE)


)


network

Graph

Gabor Szalai
  • 135
  • 1
  • 11
  • It's easier to help you if you include a [minimal, reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data and the code you are currently using. – MrFlick Feb 11 '17 at 00:21
  • Thanks for your suggestion @MrFlick, I added the code. – Gabor Szalai Feb 11 '17 at 11:40
  • could you be a bit more specific when you say: provide custom text for the hoverinfo – MLavoie Feb 11 '17 at 14:45
  • @MLavoie - I added some extra details. Hope it makes it clearer. – Gabor Szalai Feb 11 '17 at 19:52
  • If you want to publish the graph via HTML you can do it by adding more variables and catching the hover event. If that's what you are looking, I can add a possible solution. With pure R/RStudio it won't work as far as I know. – Maximilian Peters Feb 12 '17 at 11:08
  • @MaximilianPeters - Thanks, I needed this for a Shiny web-app, but I think I found an ideal work-around to the problem by hiding the text for the markers and using annotations instead. – Gabor Szalai Feb 15 '17 at 09:53

1 Answers1

4

Upon further research, I think I found a potential solution by using annotations.The idea is to hide the text from the markersand instead use annotations for the node labels. This way you can include any arbitrary character vector for the hoverinfo tooltip texts.

hover_text <- rep('Game Designer/Mario Creator',11)

size = c(100,rep(40,length(vs)-1))


network <- plot_ly(type = "scatter", x = Xn, y = Yn, mode = "markers", 
                   text = hover_text, hoverinfo = "text",marker=list(size=size,color='5BC0DE')

)

edge_shapes <- list()
for(i in 1:Ne) {
        v0 <- es[i,]$V1
        v1 <- es[i,]$V2

        edge_shape = list(
                type = "line",
                line = list(color = "DF691A", width = 1),
                x0 = Xn[match(v0,names(vs))],
                y0 = Yn[match(v0,names(vs))],
                x1 = Xn[match(v1,names(vs))],
                y1 = Yn[match(v1,names(vs))],
                opacity = 0.3
        )

        edge_shapes[[i]] <- edge_shape}

network <- layout(
        network,
        paper_bgcolor="#4e5d6c",
        plot_bgcolor="#4e5d6c",
        hovermode = "closest",
        title = paste("Miyamoto's Relations",sep = ""),
        titlefont=list(color="white"),
        shapes = edge_shapes,
        xaxis = list(title = "", showgrid = FALSE, showticklabels = FALSE, zeroline = FALSE),
        yaxis = list(title = "", showgrid = FALSE, showticklabels = FALSE, zeroline = FALSE),
        annotations=list(x = Xn,
                y = Yn,
                text = names(vs),
                xref = "x",
                yref = "y",
                showarrow = F,
                font=list(family = "calibri",
                          size = 15,
                          color = "white",
                          opacity = 1))
        )


network

enter image description here

Gabor Szalai
  • 135
  • 1
  • 11