通过 R 中的图表可视化两个离散变量之间的关联
Visualizing an association btw two discrete variables via diagram in R
假设有两个变量(state
和 group
)。 state
(s) 的实例可能与 group
(g) 的特定实例共享 属性 .例如,s1、s2 和 s3 可能与 g1[ 有关联=29=].
我想以如下图所示的图表形式可视化两个变量之间的关联:
我想用 R 生成这样的图表。你会推荐我使用什么 R 包?
根据lukeA的建议,我想出了以下代码来解决上述需求。
在Shell中:
$ cat table
s1 g1
s2 g1
s3 g1
s4 g2
s5 g2
s6 g2
s7 g3
s8 g4
s9 g5
s10 g5
在 R 中:
library(igraph)
# Reading data from file
m <- as.matrix(read.table(file="~/Desktop/table", sep="\t"))
# Generating igraph
g <- graph_from_edgelist(m, directed=FALSE)
V(g)$type <- bipartite.mapping(g)$type
coords <- layout_as_bipartite(g)
# Plotting operations
plot.igraph(g, layout = -coords[,2:1]) # Preliminary plotting (why necessary?)
plot.igraph(g, layout = -coords[,2:1],
vertex.shape="rectangle", # For vertex.foo and edge.foo commands, see: http://igraph.org/r/doc/plot.common.html
vertex.size=50,
vertex.size2=20,
vertex.color=NA,
vertex.label.color= "black")
# Adding title to plot
title("My first igraph")
假设有两个变量(state
和 group
)。 state
(s) 的实例可能与 group
(g) 的特定实例共享 属性 .例如,s1、s2 和 s3 可能与 g1[ 有关联=29=].
我想以如下图所示的图表形式可视化两个变量之间的关联:
我想用 R 生成这样的图表。你会推荐我使用什么 R 包?
根据lukeA的建议,我想出了以下代码来解决上述需求。
在Shell中:
$ cat table
s1 g1
s2 g1
s3 g1
s4 g2
s5 g2
s6 g2
s7 g3
s8 g4
s9 g5
s10 g5
在 R 中:
library(igraph)
# Reading data from file
m <- as.matrix(read.table(file="~/Desktop/table", sep="\t"))
# Generating igraph
g <- graph_from_edgelist(m, directed=FALSE)
V(g)$type <- bipartite.mapping(g)$type
coords <- layout_as_bipartite(g)
# Plotting operations
plot.igraph(g, layout = -coords[,2:1]) # Preliminary plotting (why necessary?)
plot.igraph(g, layout = -coords[,2:1],
vertex.shape="rectangle", # For vertex.foo and edge.foo commands, see: http://igraph.org/r/doc/plot.common.html
vertex.size=50,
vertex.size2=20,
vertex.color=NA,
vertex.label.color= "black")
# Adding title to plot
title("My first igraph")