在R图表中绘制交互作用

7

我有以下类型的数据(尽管数据点的数量非常大)

# property data
name <- c("A", "B", "C", "D")
diameter <- c(4.3, 8.3,1.2, 3.3)
X <- c( 1, 2, 3, 4)
Y <- c(1, 3, 3, 4)
colr <- c(10, 20, 34, 12)
propdata <- data.frame (name, diameter, X, Y, colr)


# interaction data
name1 <- c("A", "A", "A", "B", "B")
name2 <- c("B", "C", "D", "C", "D")
score <- c(1.1, 2.2, 5.4, 3.1, 2.0)
relation <- data.frame (name1, name2, score)

我想创建一个类似于以下的图表,使其具有以下属性。

(

1) diameter of circles is governed by propdata$diameter 
(2) Position in xy field is governed by cordinates of propdata$X and propdata$y 
(3) Fill color of the circle is controlled by propdata$colr 
 (4) Interaction is governed by relation data.frame, name1 
  and name2 elements will be connected and weight of the line is govenmened 
  by relation$score.

enter image description here

是否可以使用现有的R基础或任何流行的图形软件创建这样的图表,或者需要更专业的软件。

编辑:

使用气泡图,我只能做到这一步:

 p <- ggplot(propdata, aes(X,Y,size = diameter, label=name))
p <- p + geom_point(colour= "red")+geom_text(size=3) # colour = colr does not work 
p

enter image description here


答案是肯定的。你尝试了什么? - Andrie
我尝试了ggplot2,但结果不显著。我使用了geom_point()来调整气泡大小,但不知道如何连接它们... - jon
@Andrie 请看我的修改,这是我所能做到的,我已经尝试了...谢谢。 - jon
这比我最初想象的更难了。回答已发布并进行了编辑。 - Andrie
2个回答

5
这是我快速用qgraph制作的内容:
library("qgraph")

plot(1,type='n',xlim=c(min(propdata$X)-0.5,
    max(propdata$Y)+0.5),ylim=c(min(propdata$Y)-0.5,max(propdata$Y)+0.5),
    xlab="",ylab="")

col <- rgb(0,1-propdata$colr/max(propdata$colr),0)

qgraph(relation,plot=FALSE,rescale=FALSE,layout=as.matrix(propdata[c("X","Y")]),
        edge.color="darkred",color=col,propdata$colr,directed=FALSE,esize=10,
       vsize=propdata$diameter+2,lcolor="white",curve=c(0,0,-0.2,0,0))

enter image description here


非常感谢您的回答,太好了!我看了一下qgraph网页https://sites.google.com/site/qgraphproject/examples...非常不错... - jon
非常好。我要学习的新事物清单每天都在增长。 - Andrie
谢谢!如果节点的位置不重要,我会使用力导向算法。此外,默认的边缘颜色效果更好,但它们是绿色的 :) - Sacha Epskamp

3
所以,问题在于创建一个代表交互的线图。
为了做到这一点,您需要以适当的形式格式化数据。您想要绘制线段,因此您需要在数据框中将每个线段的坐标放在单独的行中。
interaction <- merge(propdata, relation, by.x="name", by.y="name1")
interaction <- cbind(interaction, 
               merge(propdata, relation, by.x="name", by.y="name2")[, c("X", "Y")])
names(interaction)[8:9] <- c("Xend", "Yend")
interaction

  name diameter X Y colr name2 score Xend Yend
1    A      4.3 1 1   10     B   1.1    2    3
2    A      4.3 1 1   10     C   2.2    3    3
3    A      4.3 1 1   10     D   5.4    3    3
4    B      8.3 2 3   20     C   3.1    4    4
5    B      8.3 2 3   20     D   2.0    4    4

现在我们遇到另一个问题。 在ggplot2中,您只能拥有单个大小比例尺。由于您同时具有点和线条的大小参数,这实际上代表了两件事,所以如果没有解决方法,这是无法完成的。
因此,解决方法是使用geom_polygon手动绘制圆圈。
用圆圈构建数据框架:
circle <- function(x, y, d, color, scale=1){
  d <- d * scale
  angle <- seq(-pi, pi, length = 50) 
  data.frame(
    x = x + d/2*sin(angle), 
    y = y + d/2*cos(angle),
    color=color)
}

circles <- ddply(propdata, .(name), 
                 function(x)with(x, circle(X, Y, diameter, colr, scale=0.2)))

最后,创建绘图:

ggplot() + 
  geom_polygon(data=circles, aes(group=name, x=x, y=y, fill=color)) +
  geom_text(data=propdata, aes(x=X, y=Y, label=name), hjust=0, vjust=0) +
  geom_segment(data=interaction, aes(x=X, y=Y, xend=Xend, yend=Yend, size=score)) + 
  scale_size("Inter", to=c(0, 5)) +
  coord_equal()

enter image description here


非常好...唯一的问题是我想把标签离圆心远一点...感谢您的回答... - jon
另外,我们能否控制行大小?有时候它会变得太粗,虽然很信息化但不美观。谢谢回答。 - jon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接