2015-11-02 67 views
0

我只想表示一系列不同顏色的分類狀態。R - 個人分類圖

這種情節也被稱爲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

任何你已經嘗試過? – Heroka

+0

沒有其他 - 我不知道從哪裏開始 – giacomo

+0

我認爲你需要搜索的詞是'heatmap'/ ggplot2。 – Heroka

回答

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