与 'text' 参数不同的 plotly R 图表上的自定义 hoverinfo 文本?
Custom hoverinfo text on plotly R chart that is different to the 'text' parameter?
我一直在尝试使用 plotly
R 库创建网络图。我想可视化自定义 hoverinfo
文本,该文本 不同于为标记(或本例中的节点)定义的文本标签 。
基本上,我想保留标记(节点)上的文本标签,因为它们是当前的,但也能够为 hoverinfo
提供自定义文本。
正如我希望工具提示显示一些不同于标记标签文本的其他字符向量。例如,当我将鼠标悬停在中央 'Shigeru Miyamoto' 节点上时,我想通过工具提示显示我可能收集的其他信息(例如类型、国籍等)。我从 plotly 文档中了解到,目前您只能为标记和 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
经过进一步研究,我想我找到了一个使用 annotations
的潜在解决方案。我的想法是隐藏 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
我一直在尝试使用 plotly
R 库创建网络图。我想可视化自定义 hoverinfo
文本,该文本 不同于为标记(或本例中的节点)定义的文本标签 。
基本上,我想保留标记(节点)上的文本标签,因为它们是当前的,但也能够为 hoverinfo
提供自定义文本。
正如我希望工具提示显示一些不同于标记标签文本的其他字符向量。例如,当我将鼠标悬停在中央 'Shigeru Miyamoto' 节点上时,我想通过工具提示显示我可能收集的其他信息(例如类型、国籍等)。我从 plotly 文档中了解到,目前您只能为标记和 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
经过进一步研究,我想我找到了一个使用 annotations
的潜在解决方案。我的想法是隐藏 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