3

我的数据看起来像这样:

在此处输入图像描述

的输出dput(sequence_data)

    structure(list(Obs = 1:13, Seq.1 = structure(c(1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L), .Label = c("a", "b", "c"
), class = "factor"), Seq.2 = structure(c(1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("c", "d"), class = "factor"), 
    Seq.3 = structure(c(1L, 1L, 1L, 2L, 1L, 1L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L), .Label = c("", "d", "e"), class = "factor"), 
    Seq.4 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
    1L, 1L, 2L), .Label = c("", "f"), class = "factor")), .Names = c("Obs", 
"Seq.1", "Seq.2", "Seq.3", "Seq.4"), class = "data.frame", row.names = c(NA, 
-13L))

我正在尝试获取过渡状态图。这是代码:

transitions <- table(sequence_data$Seq.1,sequence_data$Seq.2) %>%
getRefClass("Transition")$new(label=c("1st Iteration", "2nd Iteration"))
transitions$box_width = 0.25;
transitions$box_label_cex = 0.7;
transitions$arrow_type = "simple";
transitions$arrow_rez = 300;
table(sequence_data$Seq.2,sequence_data$Seq.3) %>% transitions$addTransitions(label = '3rd Iteration')
transitions$render()

这是输出: 在此处输入图像描述

可以从 te 图中删除空值以使其看起来更干净吗?我试图删除但表语句需要值具有相同的长度。

我正在library(Gmisc)为图表使用 GMISC 包 ( )。

谢谢

4

2 回答 2

3

这可能有点hacky,但会让你到达那里。基本上,您手动将转换 ( transitions$transitions) 设置为0.

transitions$transitions[[2]][1,1] = 0
transitions$transitions[[2]][2,1] = 0
transitions$render()

也许这个循环可以将所有值0自动更改(虽然我还没有检查过大数据)

for (level_n in 1:length(transitions$transitions)){
    x =  transitions$transitions[[level_n]]
    for (cols in 1:ncol(transitions$transitions[[level_n]])){            
        if (dimnames(x)[[2]][cols] == ""){
            transitions$transitions[[level_n]][,cols] = 0
        }
    }
}

在此处输入图像描述

于 2017-01-20T20:45:05.187 回答
3

您可以使用lapply将列名称为空字符串的每个列的值设置为零。然后当你运行时transition$render(),空的转换就会消失。起初我想,这可以简单地完成如下:

# Set transitions table columns with a blank name to zeros
transitions$transitions = lapply(transitions$transitions, function(tab) {
  tab[ , which(colnames(tab)=="")] = 0
  tab
})

但是,lapply从输出列表中删除“转换”属性,导致错误(如果有人知道解决此问题的方法,请告诉我)。因此,我将更新后的列表保存在一个名为 的临时对象中tmp,恢复“transitions”属性,然后重置 的值transitions$transitions

# Set transitions table columns with a blank name to zeros
tmp = lapply(transitions$transitions, function(tab) {
  tab[ , which(colnames(tab)=="")] = 0
  tab
})

# Restore "transition" attribute
attributes(tmp)$transitions = TRUE

# Set transitions to the new values we just created
transitions$transitions = tmp

在此处输入图像描述

当我研究这个时,我想知道空白转换值应该发生什么。上图似乎具有误导性,因为它似乎表明c第二次迭代的d所有值都去了,第二次迭代的所有d值都去了e。但实际上,13 个值中有 5 个去了""(即空字符串)。他们只是消失了吗?如果是这样,第 3 次迭代条的总高度不应该是第 1 次和第 2 次迭代条高度的 7/13 吗?或者也许尝试这样的事情,只是为了表明一些价值转变为遗忘:

transitions$fill_clr[[3]] = c("white", transitions$fill_clr[[3]][-1])
transitions$render()

在此处输入图像描述

或者,空白是否真的代表从第二次到第三次转换保持不变的值?如果是这种情况,那么用上一次转换中各自的值填充空白值可能会更好。这种情况的图表如下所示:

library(zoo)

# Convert empty values to NA
sequence_data[sequence_data==""] = NA

# Fill NA values with last value carried forward
sequence_data=as.data.frame(t(apply(sequence_data, 1, na.locf)))

transitions <- table(sequence_data$Seq.1, sequence_data$Seq.2) %>%
getRefClass("Transition")$new(label=c("1st Iteration", "2nd Iteration"))
transitions$box_width = 0.25;
transitions$box_label_cex = 1;
transitions$box_cex = 2;
transitions$arrow_type = "simple";
transitions$arrow_rez = 300;
table(sequence_data$Seq.2,sequence_data$Seq.3) %>% transitions$addTransitions(label = '3rd Iteration')
transitions$render()

在此处输入图像描述

于 2017-01-22T07:13:22.787 回答