R - 个人分类图

时间:2015-11-02 11:33:55

标签: r plot ggplot2 sequence data-visualization

我只想表示一系列具有不同颜色的分类状态。

这种情节也称为individual sequence plotTraMineR)。

我想使用ggplot2

我的数据看起来像这样

> head(dta)
  V1 V2 V3 V4 V5 id
1  b  a  e  d  c  1
2  d  b  a  e  c  2
3  b  c  a  e  d  3
4  c  b  a  e  d  4
5  b  c  e  a  d  5

在最后一列中使用personal id

情节看起来像这样。

enter image description here

每个letters(状态)由颜色表示。基本上,该图可视化每个人的连续状态。

蓝色为a,红色为b,紫色为c,黄色为d,棕色为e

知道如何使用ggplot2执行此操作?

dta = structure(list(V1 = structure(c(1L, 3L, 1L, 2L, 1L), .Label = c("b", 
"c", "d"), class = "factor"), V2 = structure(c(1L, 2L, 3L, 2L, 
3L), .Label = c("a", "b", "c"), class = "factor"), V3 = structure(c(2L, 
1L, 1L, 1L, 2L), .Label = c("a", "e"), class = "factor"), V4 =  structure(c(2L, 
3L, 3L, 3L, 1L), .Label = c("a", "d", "e"), class = "factor"), 
V5 = structure(c(1L, 1L, 2L, 2L, 2L), .Label = c("c", "d"
), class = "factor"), id = 1:5), .Names = c("V1", "V2", "V3", 
"V4", "V5", "id"), row.names = c(NA, -5L), class = "data.frame")

到目前为止我尝试了什么

nr = nrow(dta3)
nc = ncol(dta3)

# space 
m = 0.8
n = 1 # do not touch this one 

plot(0, xlim = c(1,nc*n), ylim = c(1, nr), type = 'n', axes = F, ylab = 'individual sequences', xlab = 'Time')

axis(1, at = c(1:nc*m), labels = c(1:nc))
axis(2, at = c(1:nr), labels = c(1:nr) )

for(i in 1:nc){
  points(x = rep(i*m,nr) , y = 1:nr, col = dta3[,i], pch = 15) 
}

但它不是ggplot2而不是非常令人满意。

enter image description here

1 个答案:

答案 0 :(得分:3)

你走了:

library(reshape2)
library(ggplot2)

m_dta <- melt(dta,id.var="id")
m_dta

p1 <- ggplot(m_dta,aes(x=variable,y=id,fill=value))+
  geom_tile()
p1

enter image description here