gpt4 book ai didi

r - 与 'text' 参数不同的 plotly R 图表上的自定义 hoverinfo 文本?

转载 作者:行者123 更新时间:2023-12-04 09:36:20 25 4
gpt4 key购买 nike

关闭。这个问题需要debugging details .它目前不接受答案。












想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。

4年前关闭。




Improve this question




我一直在尝试使用 plotly 创建网络图R 库。我想可视化一个自定义 hoverinfo文本是 不同于文本标签为标记(或本例中的节点)定义。

基本上,我想将文本标签保留在标记(节点)上,因为它们目前是这样,但也能够为 hoverinfo 提供自定义文本。 .
正如我希望工具提示显示与标记的标签文本不同的一些其他字符向量。例如,当我将鼠标悬停在中央“Shigeru Miyamoto”节点上时,我希望工具提示显示我可能收集的其他信息(例如类型、国籍等)。我从 plotly 文档中的理解是,目前您只能为标记和悬停信息工具提示显示相同的文本标签。

有没有办法做到这一点?

这是我使用的代码:

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

最佳答案

经过进一步研究,我想我通过使用 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

enter image description here

关于r - 与 'text' 参数不同的 plotly R 图表上的自定义 hoverinfo 文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42170476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com