表示数据框
df <- data.frame(g1=c("x1","x2","x2","x3","x4","x5","x5","x3"),
g2=c("y1","y4","y2","y4","y3","y4","y5","y4"),
g1value=c(1,2,2,3,4,5,5,3),
g2value=c(2,4,2,4,5,4,NA, 4),
stringsAsFactors = FALSE)
library(igraph)
g=graph.data.frame(df, directed=F)
plot(g)
如何指定节点的大小,使得x节点的每个大小对应于g1value,y节点对应于g2value(如果节点大小为na则忽略该节点)?
以及如何在条件下改变节点边界颜色或厚度,例如节点是x2?
如果有很多节点,并且绘图结果很拥挤,那么无论如何要将它分开?
感谢。
答案 0 :(得分:0)
这是一个开始 - 您可以将不同大小和颜色的向量传递给plot.igraph
的参数,以便它们适用于不同的节点。它们按V(g)$name
的顺序给出。
# tweaking your data to increase node size
df <- data.frame(g1=c("x1","x2","x2","x3","x4","x5","x5","x3"),
g2=c("y1","y4","y2","y4","y3","y4","y5","y4"),
g1value=10*c(1,2,2,3,4,5,5,3),
g2value=10*c(2,4,2,4,5,4,NA, 4),
stringsAsFactors = FALSE)
library(igraph)
g <- graph.data.frame(df[,1:2], directed=F)
# create a vector of vertex sizes conditional on g*values in df
# set missing values to size 0
r <- data.frame(g=c(df$g1, df$g2), value=c(df$g1value, df$g2value))
sz <- r$value[match(V(g)$name, r$g)]
sz[is.na(sz)] <- 0
# create a vertor of border colours conditional on node type
bd <- ifelse(grepl("x", V(g)$name), "red", NA)
# add the size and border colour vectors to relevant plot arguments
plot(g, vertex.size=sz, vertex.frame.color=bd)
igraph
有很好的帮助页面,有关绘图选项/参数,请参阅?igraph::layout
了解图表布局选项,?plot.igraph
和?igraph.plotting
。
对于边框宽度,请关注this link。