plotly R图表上的自定义悬停信息文本与“文本"参数不同?

编程入门 行业动态 更新时间:2024-10-11 03:21:09
本文介绍了plotly R图表上的自定义悬停信息文本与“文本"参数不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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).

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

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?

这是我使用的代码:

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 工具提示文本包含任意字符向量.

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

更多推荐

plotly R图表上的自定义悬停信息文本与“文本"参数不同?

本文发布于:2023-07-19 13:48:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1157123.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本   自定义   图表   参数   信息

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!