使用R

时间:2017-11-01 20:55:31

标签: r data-visualization

我正在寻找为有限状态机生成转换图的建议和建议。我希望在每个页面上放置两个图表并将结果导出为PDF文件。问题是,无论我调整plotmat函数中的参数,我总是在输出中得到重叠的标签。

我想知道:

  1. 是否有人熟悉diagram包裹?它是否支持布局选项(例如,强制地图集)?

  2. 在R中生成类似的转换矩阵有没有更好的选择? (我知道我们可以在python中做很多事情......但我现在必须处理R)

  3. 这是一个玩具示例:

    pdf('toy_ex.pdf')
    par(mfrow = c(2,1))
    mat1 <- matrix(c(0,0,0.5,0.25,0,0,0,0,0,0,0,0.5,0.5,0,0,0,0.5,0,0.5,1,0,0,0,0.25,0), nrow = 5, byrow = TRUE)
    plotmat(mat1, relsize = 0.6, shadow.size = 0, cex.txt = 0.6, box.cex = 0.6, self.cex = 0.6, self.shiftx = -0.1, arr.type = 'simple', arr.length = 0.2, name = c('None', 'Raccoon', 'Giraffe','Lion','Oyster'), main = 'Transition', cex.main = 1)
    mat2 <- mat1
    plotmat(mat2, relsize = 0.6, shadow.size = 0, cex.txt = 0.6, box.cex = 0.6, self.cex = 0.6, self.shiftx = -0.1, arr.type = 'simple', arr.length = 0.2, name = c('None', 'Raccoon', 'Giraffe','Lion','Oyster'), main = 'Transition', cex.main = 1)
    dev.off()
    

    这就是输出的样子。我知道它很乱......

    enter image description here

    谢谢!

2 个答案:

答案 0 :(得分:0)

也许尝试使用igraph

例如

library(igraph)
rownames(mat1) = c('None', 'Raccoon', 'Giraffe','Lion','Oyster')
colnames(mat1) = c('None', 'Raccoon', 'Giraffe','Lion','Oyster')
g <- graph_from_adjacency_matrix(mat1, weighted = "prob")
plot(g, 
     edge.label = E(g)$prob,
     edge.arrow.size = 1,
     edge.label.cex = 1.5,
     edge.curved = TRUE,
     layout = layout.fruchterman.reingold,
     vertex.size = 45,
     vertex.color="lightblue",
     vertex.frame.color= "black",
     vertex.label.color = "black",
     vertex.label.family = "sans",
     edge.width = 1.5,  
     edge.color="grey50", 
     edge.label.dist = 0)

enter image description here

我刚刚意识到所有方向都已颠倒过来 - 快速解决方法是使用t(mat1),因为plotmat使用(rows=to, cols=from)igraph使用(rows=from, cols=to)

答案 1 :(得分:0)

您可以更改颜色,使其看起来更好:

require(diagram)

plotmat(mat2, relsize = 0.6, shadow.size = 0, cex.txt = 0.6, box.cex = 0.6,
  self.cex = 0.6, self.shiftx = -0.1, arr.type = 'simple', arr.length = 0.2, 
  name = c('None', 'Raccoon', 'Giraffe','Lion','Oyster'), main = 'Transition', cex.main = 1,
  txt.col="black",box.col="lightblue", lcol="grey")