在r中连接像树一样的线

fpr*_*prd 6 plot r ggplot2

我有以下人类家庭的类型数据:

indvidual <- c("John",  "Kris", "Peter",  "King",  "Marry",  "Renu", "Kim",    "Ken", "Lu")
Parent1 <- c(    NA,     NA,     "John",  "John",   "John",    NA,    "Peter",  NA,    NA)
Parent2 <- c(    NA,     NA,    "Kris",   "Kris",  "Renu",   NA,      "Lu",     NA,   NA)
X <-       c(    2,     3,       2,       3,           4,     5,        1.5,      1,    1)
Y <-       c(    3,     3,       2,       2,           2,     3,        1,      3,    2)
pchsize <- c( 4.5,      4.3,     9.2,     6.2,         3.2,   6.4,      2.1,    1.9,  8)
fillcol <- c( 8.5,      8.3,     1.2,     3.2,         8.2,   2.4,      2.6,    6.1,  3.2)
myd <- data.frame (indvidual, Parent1, Parent2, X, Y, pchsize,fillcol)

 indvidual Parent1 Parent2   X Y pchsize fillcol
1      John    <NA>    <NA> 2.0 3     4.5     8.5
2      Kris    <NA>    <NA> 3.0 3     4.3     8.3
3     Peter    John    Kris 2.0 2     9.2     1.2
4      King    John    Kris 3.0 2     6.2     3.2
5     Marry    John    Renu 4.0 2     3.2     8.2
6      Renu    <NA>    <NA> 5.0 3     6.4     2.4
7       Kim   Peter      Lu 1.5 1     2.1     2.6
8       Ken    <NA>    <NA> 1.0 3     1.9     6.1
9        Lu    <NA>    <NA> 1.0 2     8.0     3.2
Run Code Online (Sandbox Code Playgroud)

我希望情节类似于以下内容,个人点与父母相连(最好列出与Parent1和Parent2不同的线条颜色).pch size和pch fill也会缩放到其他变量pchsize和fillcol.因此情节大纲是:

在此输入图像描述

这是我在ggplot2中的进展:

require(ggplot2) 
ggplot(data=myd, aes(X, Y,fill = fillcol)) +
  geom_point(aes(size = pchsize, fill = fillcol), pch = "O") +
  geom_text(aes (label = indvidual, vjust=1.25))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

未解决的问题:连接线,使pch大小同时填充颜色.

Win*_*ine 2

我突然想到的一件事是把它当作一个网络——R 有很多包来绘制这些网络。

这是一个非常简单的解决方案:首先,我使用您的父母列表来制作社会矩阵 - 您通常也可以使用边缘列表输入网络 - 这里我为第一个父母关系输入 1,为第二个父母关系输入 2。

psmat <- rbind(c(0, 0, 1, 1, 1, 0, 0, 0, 0),
          c(0, 0, 2, 2, 0, 0, 0, 0, 0),
          c(0, 0, 0, 0, 0, 0, 1, 0, 0),
          rep(0, 9),
          rep(0, 9),
          c(0, 0, 0, 0, 2, 0, 0, 0, 0),
          rep(0, 9),
          rep(0, 9),
          c(0, 0, 0, 0, 0, 0, 2, 0, 0))
Run Code Online (Sandbox Code Playgroud)

然后,使用我刚刚点击的网络包:

require(network)
plot(network(psmat), coord = cbind(X, Y), vertex.cex = pchsize, 
  vertex.col = fillcol, label = indvidual, edge.col = psmat)
Run Code Online (Sandbox Code Playgroud)

这本身并不是很漂亮,但我认为它为您提供了您想要的所有基本元素。

对于颜色,我相信小数位只是四舍五入 - 我不知道如何处理这些。

我知道我见过人们在 ggplot 中绘制网络,所以这可能会给你一个更好的结果。

示例图片

编辑:所以这是一种将数据直接转换为网络对象的非常混乱的方法 - 其他人可能能够修复它。此外,我添加了一个边缘属性(名为“P”表示育儿状态),并将第一组的值指定为 1,将第二组的值指定为 2。这可以在绘图时使用以设置颜色。

P1 <- match(Parent1, indvidual)
e1 <- cbind(P1, 1:9); e1 <- na.omit(e1); attr(e1, 'na.action') <- NULL
P2 <- match(Parent2, indvidual)
e2 <- cbind(P2, 1:9); e2 <- na.omit(e2); attr(e2, 'na.action') <- NULL

en1 <- network.initialize(9)
add.edges(en1, e1[,1], e1[,2])
set.edge.attribute(en1, 'P', 1)
add.edges(en1, e2[,1], e2[,2], names.eval = 'P', vals.eval = 2)

plot(en1, coord = cbind(X, Y), vertex.cex = pchsize, 
  vertex.col = fillcol, label = indvidual, edge.col = 'P')
Run Code Online (Sandbox Code Playgroud)