R - 个人分类图

R - individual categorical plot

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

这种情节也被称为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

剧情是这样的。

每个 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 不同,不是很令人满意。

给你:

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