在R中使用geom_net时显示不正确 [英] Inproper show when use geom_net in R

查看:77
本文介绍了在R中使用geom_net时显示不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出如下数据框:

v1     v2     v3     v4
Tom     A     Jim     B
Gary    A     Shirly  A
Shirly  B     Jack    B
Tom     A     Jack    B
...

v2和v4表示v1和v3中的名称分别属于哪个组。 Tom属于A组,Jim属于v4组。
我想用 geom_net 绘制一个社交网络,如果两个名字在同一行,则将它们链接到两个名字,例如, Tom Jim 。边缘的大小应与它们在V3中出现的时间成正比,即 Jack 的边缘应该是 Jim的两倍大 Shirly

v2 and v4 denote which group the name in v1 and v3 respectively belongs to. Tom belongs to group A and Jim belongs to group v4. I'd like to plot a social network with geom_net, with lines linkage to two names if they are in the same row, for instance, Tom and Jim. And the size of edges should be proportional to the times they have been appeared in V3, i.e, the edge of Jack should be as twice big as Jim and Shirly.

我尝试过

ggplot(df, aes(from_id = V1,to_id = V3)) +geom_net()

但是给出的结果非常糟糕:

But a very bad result is given:

并生成警告:

In f(..., self = self) :
There are 35 nodes without node information:
#And the below are all the values in V1 and V3
Tom, Shirly, ....
Did you use all=T in merge?

我想知道如何在没有x轴或y的情况下以正确且美观的方式显示结果轴和边缘之间的关系应该清楚地显示出来。边缘的颜色应代表它们所属的组。这意味着同一组中的所有名称都应具有相同的颜色。

I wonder how to show the result in a proper and good looking way with no x-axis or y-axis and the relationship among edges should be clearly shown. And the edges' color should represent the groups they belongs to. That means all names in the same group should have same color.

希望得到您的帮助!

推荐答案

我也一直为此苦苦挣扎,直到我弄清楚了geom_net包的正确data.frame结构是什么。基本上,您需要一个包含两部分的data.frame:在第1部分中,您将通过提供FROM和TO列来描述边(绘制的线)。 (可选)可以在单独的列中提供其他信息,例如,线宽

I struggled with this too until I figured out what the correct data.frame structure was for the geom_net package. Basically what you need is a data.frame that has two parts: in part 1 you describe the edges (the lines drawn) by providing a FROM and a TO column. Optionally, additional info can be provided in a separate column e.g., linewidth

ans <- read.table(text ="
from to linewidth
Tom Jim 0.1
Gary Shirly 1
Shirly Jack 0.5
Tom Jack 2
", sep = " ", stringsAsFactors = FALSE, header=TRUE)

p <- ggplot(data = ans, aes(from_id = from, to_id = to))
p + geom_net(label = TRUE, vjust=-1)

但是您会注意到某些节点(顶点)没有被标记。因此,这是data.frame的第2部分很重要的地方。在第2部分中,您将提供要标记的节点的名称。这是因为geom_net仅标记FROM节点,而不标记TO节点,因此您至少需要提供未被用作FROM点的节点的名称。

But you will notice that some of the nodes (vertices) are not labelled. So this is where part 2 of the data.frame is important. In part 2 you supply the names of the nodes to be labelled. This is because geom_net only labels the FROM node and not the TO node, so you will need to supply, as a minimum, the names of the nodes that are not used as a FROM point.

ans <- read.table(text ="
from to linewidth
Tom Jim 0.1
Gary Shirly 1
Shirly Jack 0.5
Tom Jack 2
Helen Jack 3
Jim NA NA
Jack NA NA
", sep = " ", stringsAsFactors = FALSE, header=TRUE, na.strings = "NA")

p <- ggplot(data = ans, aes(from_id = from, to_id = to, linewidth = linewidth))
p + geom_net(label = TRUE, vjust=-1)

上面发生的几件事:1)我添加了 Jim NA NA Jack NA NA作为未标记节点的标签,2)还添加了na.strings = NA以确保read.table()正确解释了NA值,以及3)我将linewidth参数添加到aes以便其映射从data.frame到图。

Several things going on above: 1) I added "Jim NA NA Jack NA NA" as labels for the unlabeled nodes, 2) also added na.strings = "NA" to ensure that read.table() properly interprets the NA values, and 3) I added the linewidth parameter to the aes so that it maps from the data.frame to the plot.

此外,一旦为所有节点提供名称,警告消息就有XX个没有节点信息的节点就会消失。

Also, once you supply names for all the nodes, the warning message "There are XX nodes without node information" goes away.

希望有帮助

编辑:根据要求,我添加了结果输出。由于geom_net()每次运行都会更改布局,因此我提供了两个示例图像

Hope that helps edit: as requested I added the resultant output. Since geom_net() changes the layout each time it is run, I have included two example images

只是为了完成整个操作data.frame的构建过程,在下面的示例中,您有两个单独的data.frame,需要将它们合并在一起:第一个data.frame用于线条(边),第二个是节点(顶点)。 / p>

Just to complete the whole data.frame building process, I have included below a case where you have two separate data.frames and you need to merge them together: first data.frame is for the lines (edges) and the second is the nodes (vertices).

lines <- read.table(text ="
from to linewidth
Tom Ivy 0.1
Gary Ivy 1
Shirly Ivy 0.5
Tom Helen 2
Helen Ivy 3
", sep = " ", stringsAsFactors = FALSE, header=TRUE, na.strings = "NA")

nodes <- read.table(text ="
name
Tom
Jim
Gary
Shirly
Jack
Helen
Susan
Joel
Ivy
", sep = " ", stringsAsFactors = FALSE, header=TRUE,na.strings = "NA")

df <- merge(lines, nodes, by.x = "from", by.y = "name", all = TRUE)

p <- ggplot(data = df, aes(from_id = from, to_id = to, linewidth = linewidth))
p + geom_net(label = TRUE, vjust=-1)

这篇关于在R中使用geom_net时显示不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆