简体   繁体   English

visNetwork R:指定变量节点背景颜色和边框颜色

[英]visNetwork R: Specify variable node background colour and border colour

I would like to be able to vary the background colour of the nodes in my graph, and also use a border colour to ensure nicely marked-out nodes compared to edges.我希望能够改变图表中节点的背景颜色,并使用边框颜色来确保与边缘相比很好地标记节点。

I thought that the following should work, but it does not:我认为以下应该有效,但它没有:

nodes <- data.frame(id = 1:3,
                    color = c("lightblue","red","blue"))
edges <- data.frame(from = c(1,2), to = c(1,3))
visNetwork(nodes, edges) %>% visNodes(color = list(border = "black"))

Resultant network结果网络

在此处输入图像描述

I tried providing a vector of colors directly to visNodes, which failed.我尝试将 colors 的向量直接提供给 visNodes,但失败了。 Indeed the colors are retained from the data.frame column color实际上 colors 保留在 data.frame 列颜色中

visNetwork(nodes, edges) %>% visNodes(color = list(background = c("yellow","pink","blue"), 
border = "black"))

Resultant network结果网络

在此处输入图像描述

visNetwork is a great package but is under queried on StackOverflow. visNetwork 是一个很棒的 package,但正在 StackOverflow 上进行查询。

I'm posting this here on behalf of Charlie from GitHub after asking for due permission, primarily to have this available on StackOverflow for other R viz enthusiasts to get their answers here.在获得适当许可后,我代表GitHub 的 Charlie在此处发布此信息,主要是为了在 StackOverflow 上为其他 R 即爱好者提供此信息,以便在这里获得答案。

I had to post hyperlinks to pictures since I'm new to StackOverflow with a profile (Have used it for years as an Analytics Consultant, but didn't think about creating a profile, didn't know the use for it) and am not allowed to post images before collecting 10 reputation points.我不得不发布图片的超链接,因为我是 StackOverflow 的新手,有一个配置文件(多年来一直使用它作为分析顾问,但没有考虑创建配置文件,不知道它的用途)并且不是允许在收集 10 个声望点之前发布图像。

This case is interesting.这个案例很有趣。 First, we can pass individual options to nodes/edges data.frame, and global option for all nodes / edges with visNodes and visEdges.首先,我们可以将单个选项传递给节点/边 data.frame,并使用 visNodes 和 visEdges 为所有节点/边传递全局选项。

So you can't do that:所以你不能这样做:

visNetwork(nodes, edges) %>% visNodes(color = list(background = c("yellow","pink","blue"), 
border = "black"))

But just assign one background and border color with this way:但只需用这种方式分配一种背景和边框颜色:

visNetwork(nodes, edges) %>% visNodes(color = list(background = "blue", 
border = "black"))

color can be a simple option with one value, or have a more complex definition. color可以是具有一个值的简单选项,也可以具有更复杂的定义。 In your first example, it seems that the definition of color in data.frame overwrite the visNodes options.在您的第一个示例中,似乎 data.frame 中的color定义覆盖了visNodes选项。

Since 0.2.1, you can now pass complex options (list) directly in data.frame like this:从 0.2.1 开始,您现在可以直接在 data.frame 中传递复杂的选项(列表),如下所示:

nodes <- data.frame(id = 1:3,
                    color.background = c("lightblue","red","blue"),
                    color.border = "black")
edges <- data.frame(from = c(1,2), to = c(1,3))
visNetwork(nodes, edges)

and so:所以:

nodes <- data.frame(id = 1:3,
                    color.background = c("lightblue","red","blue"))
edges <- data.frame(from = c(1,2), to = c(1,3))
visNetwork(nodes, edges) %>% visNodes(color = list(border = "black"))

PS: Posting an answer myself since this conversation was available on GitHub issues already. PS:我自己发布一个答案,因为这个对话已经在 GitHub 问题上可用。 Just wanted to document it here for the community's ease.只是想在这里记录它以方便社区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM