使用geom_tile()在ggplot2中的序列索引图

时间:2015-06-23 14:34:40

标签: r ggplot2 traminer

我尝试使用ggplot创建序列图,以便使用序列分析在我的论文中保持相同的视觉风格。我这样做:

library(ggplot2)
library(TraMineR)
library(dplyr)
library(tidyr)    
data(mvad)    
mvad_seq<-seqdef(mvad,15:length(mvad))
mvad_trate<-seqsubm(mvad_seq,method="TRATE")
mvad_dist<-seqdist(mvad_seq,method="OM",sm=mvad_trate)
cluster<-cutree(hclust(d=as.dist(mvad_dist),method="ward.D2"),k=6)
mvad$cluster<-cluster
mvad_long<-gather(select(mvad,id,contains("."),-matches("N.Eastern"),-matches("S.Eastern")),
                    key="Month",value="state",
                    Jul.93, Aug.93, Sep.93, Oct.93, Nov.93, Dec.93, Jan.94, Feb.94, Mar.94,
                    Apr.94, May.94, Jun.94, Jul.94, Aug.94, Sep.94, Oct.94, Nov.94, Dec.94, Jan.95,
                    Feb.95, Mar.95, Apr.95, May.95, Jun.95, Jul.95, Aug.95, Sep.95, Oct.95, Nov.95,
                    Dec.95, Jan.96, Feb.96, Mar.96, Apr.96, May.96, Jun.96, Jul.96, Aug.96, Sep.96,
                    Oct.96, Nov.96, Dec.96, Jan.97, Feb.97, Mar.97, Apr.97, May.97, Jun.97, Jul.97,
                    Aug.97, Sep.97, Oct.97, Nov.97, Dec.97, Jan.98, Feb.98, Mar.98, Apr.98, May.98,
                    Jun.98, Jul.98, Aug.98, Sep.98, Oct.98, Nov.98, Dec.98, Jan.99, Feb.99, Mar.99,
                    Apr.99, May.99, Jun.99)

mvad_long<-left_join(mvad_long,select(mvad,id,cluster))
ggplot(data=mvad_long,aes(x=Month,y=id,fill=state))+geom_tile()+facet_wrap(~cluster)

我尝试按簇绘制序列,这给出了以下图:Sequence index plot with ggplot

正如您所看到的,不存在不属于每个方面所代表的群集的ID的空白。我想摆脱这些差距,以便序列显示堆叠,就像TraMineR的seqIplot()函数一样,如下图所示: SeqIplot

有关如何进行的任何建议吗?

2 个答案:

答案 0 :(得分:4)

两个小变化:

mvad_long$id <- as.factor(mvad_long$id)
ggplot(data=mvad_long,aes(x=Month,y=id,fill=state))+
       geom_tile()+facet_wrap(~cluster,scales = "free_y")

ggplot将id视为一个数值变量,而不是一个因子,然后比例被修正。

答案 1 :(得分:1)

更新:我需要将月份转换为日期才能正常工作。完整的解决方案如下:

library(ggplot2)
library(TraMineR)
library(dplyr)
library(tidyr)    
library(lubridate)

data(mvad)    

mvad_seq <- seqdef(mvad, 15:length(mvad))
mvad_trate <- seqsubm(mvad_seq, method = "TRATE")
mvad_dist <- seqdist(mvad_seq, method = "OM", sm = mvad_trate)

cluster <- cutree(hclust(d = as.dist(mvad_dist), method = "ward.D2"), k = 6)
mvad$cluster <- cluster

mvad_long <- mvad %>% 
  select(id, matches("\\.\\d\\d")) %>% 
  gather(key = "month", value = "state", -id) %>% 
  inner_join(
    mvad %>% 
      select(id, cluster),
    by = "id"
  ) %>% 
  mutate(
    id = factor(id),
    date = myd(paste0(month, "01"))
  )

mvad_long %>% 
  ggplot(aes(x = date, y = id, fill = state, color = state)) +
  geom_tile() +
  facet_wrap(~cluster, scales = "free_y", ncol = 2) +
  theme_bw() +
  theme(
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    panel.grid = element_blank()
  ) +
  scale_fill_brewer(palette = "Accent") +
  scale_colour_brewer(palette = "Accent") +
  labs(x = "", y = "")

enter image description here