【问题标题】:R - individual categorical plot [duplicate]R - 个人分类图[重复]
【发布时间】:2015-11-02 11:33:54
【问题描述】:

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

这种情节也称为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 并且不是很令人满意。

【问题讨论】:

  • 你已经尝试过什么了吗?
  • 实际上没有 - 我不知道从哪里开始
  • 我认为你需要搜索的词是'heatmap' /ggplot2。
  • 但它实际上不是热图,它不涉及计算。
  • 这里的通用方法是融合你的数据,然后使用 geom_tile。

标签: r plot ggplot2 sequence data-visualization


【解决方案1】:

给你:

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

【讨论】:

    猜你喜欢
    • 2018-08-14
    • 1970-01-01
    • 2013-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-03
    相关资源
    最近更新 更多