通过R中的图表可视化两个离散变量之间的关联 [英] Visualizing an association btw two discrete variables via diagram in R
本文介绍了通过R中的图表可视化两个离散变量之间的关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假定两个变量( state
和 group
)。 状态
( s )的实例可以与 group
( g )。例如, s1 , s2 和 s3 可能与 g1 相关。
我想以图表的形式可视化两个变量之间的关联,如下图所示:
我想用R生成这样的图表。您建议我使用哪种R包?
解决方案
基于
Assume two variables (state
and group
). Instances of state
(s) may share a property with specific instances of group
(g). For example, s1, s2 and s3 may have an association with g1.
I would like to visualize the association between the two variables in the form of a diagram like to one displayed below:
I would like to generate such a diagram with R. What R package would you recommend me to use?
解决方案
Based on the suggestion of lukeA, I came up with the following code that addresses the above-mentioned need.
In Shell:
$ cat table
s1 g1
s2 g1
s3 g1
s4 g2
s5 g2
s6 g2
s7 g3
s8 g4
s9 g5
s10 g5
In 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")
这篇关于通过R中的图表可视化两个离散变量之间的关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文