2017-02-10 62 views
1

我一直在嘗試使用plotly R庫創建網絡圖。我想要想象一個自定義的hoverinfo文本,其文本標記與爲標記(或本例中的節點)定義的文本標記不同。在與「文本」參數不同的標準R表上定製hoverinfo文本?

基本上,我想保留標記(節點)上的文本標籤,因爲它們是當前的,但也能夠爲hoverinfo提供自定義文本。 正如我希望工具提示顯示與標記的標籤文本不同的其他字符矢量。例如,當我徘徊在中央的「宮本茂」節點時,我想用工具提示來展示我可能收集的其他信息(例如類型,國籍等)。我從劇情介紹中瞭解到,目前你只能爲標記和hoverinfo工具提示顯示相同的文本標籤。

有沒有辦法做到這一點?

這是我使用的代碼:

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

+0

它更容易幫助你,如果你包括[最小,可重複的例子(http://stackoverflow.com/questions/5963269/how-to-make-a-great-r - 可重複使用的示例),包含示例輸入數據和您當前使用的代碼。 – MrFlick

+0

感謝您的建議@MrFlick,我添加了代碼。 –

+0

當你說:爲hoverinfo提供自定義文本 – MLavoie

回答

3

經進一步研究,我想我找到了一個潛在的解決方案通過使用annotations .The想法是從markers隱藏文本,而使用annotations爲節點標籤。這樣,您可以包含hoverinfo工具提示文本的任意字符向量。

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