我想创建一个像这样的序列图(第一个图,b)): http://traminer.unige.ch/preview-visualizing.shtml
但是我想为此使用ggplot2
。为此,我“融化”了数据,因为我不喜欢宽格式。现在,我用geom_raster
绘制了结果数据,结果如下:
我希望像第一个链接一样,一开始会得到六个“水平块”(我希望你知道我的意思),但是job变量已经很混乱了。
这是我的代码,我认为只有带有顺序的行和使用ggplot
的行才与问题有关:
library(TraMineR)
library(data.table)
library(magrittr)
library(zoo)
library(stringr)
library(purrr)
library(ggplot2)
Sys.setlocale("LC_ALL","English")
data(mvad)
Data <- as.data.table(mvad)
rm(mvad)
Data %<>%
melt(measure.vars = c("Belfast", "N.Eastern", "Southern", "S.Eastern", "Western"),
variable.name = "school",
value.name = "school.boolean") %>%
.[school.boolean == "yes"] %>%
.[, -"school.boolean"]
time.vars <-
names(Data) %>%
.[str_detect(., "[:alpha:]{3}\\.[:digit:]{2}")]
boolean.cols <-
c("male", "catholic", "Grammar", "funemp", "gcse5eq", "fmpr", "livboth")
Data %<>%
melt(measure.vars = time.vars,
variable.name = "month",
value.name = "job") %>%
.[, month := as.yearmon(month, "%b.%y")] %>%
setorder(id, month) %>%
.[, (boolean.cols) := map(.SD, ~ {.x == "yes"}),
.SDcols = boolean.cols] %>%
.[, Sex := ifelse(male == TRUE, "Male", "Female")] %>%
.[, -"male"] %>%
setnames(names(.), names(.) %>% str_to_title) %>%
.[, Id := factor(Id, levels = Id[order(Job, Month)] %>% unique)]
Data %>%
ggplot(aes(x = Month, y = Id, fill = Job)) +
geom_raster() +
labs(y = NULL)
编辑:.[, Id := factor(Id, levels = Id[order(Month, Job)] %>% unique)]
也不起作用。
答案 0 :(得分:0)
在定义time.vars
之后,必须由它们设置数据集的顺序。然后,一个具有正确的ID排序。
Data %<>% setorderv(time.vars)
ID.order <- Data[, id %>% unique]
最后一行
.[, Id := factor(Id, levels = Id[order(Job, Month)] %>% unique)]
必须用这个替换:
.[, Id := factor(Id, levels = ID.order)]