添加其他X轴标题后,子图会分解

时间:2018-06-22 14:50:09

标签: r ggplot2 plotly subplot

我有这个数据框:

gene_symbol<-c("DADA","SDAASD","SADDSD","SDADD","ASDAD","XCVXCVX","EQWESDA","DASDADS","SDASDASD","DADADASD","sdaadfd","DFSD","SADADDAD","SADDADADA","DADSADSASDWQ","SDADASDAD","ASD","DSADD")
panel<-c("growth","growth","growth","growth","big","big","big","small","small","dfgh","DF","DF","DF","DF","DF","gh","DF","DF")
ASDDA<-c("normal","over","low","over","normal","over","low","over","normal","over","DF","DF","DF","DF","DF","DF","DF","DF")
ASDDb<-c("normal","over","low","over","normal","over","low","over","normal","over","DF","DF","DF","DF","DF","DF","DF","DF")
ASDDAf<-c("normal","over","low","over","normal","over","low","over","normal","over","DF","DF","DF","DF","DF","DF","DF","DF")
ASDDAf1<-c("normal","over","low","over","normal","over","low","over","normal","over","DF","DF","DF","DF","DF","DF","DF","DF")
ASDDAf2<-c("normal","over","low","over","normal","over","low","over","normal","over","DF","DF","DF","DF","DF","DF","DF","DF")
ASDDAf3<-c("normal","over","low","over","normal","over","low","over","normal","over","DF","DF","DF","DF","DF","DF","DF","DF")
ASDDAf4<-c("normal","over","low","over","normal","over","low","over","normal","over","DF","DF","DF","DF","DF","DF","DF","DF")
ASDDAf5<-c("normal","over","low","over","normal","over","low","over","normal","over","DF","DF","DF","DF","DF","DF","DF","DF")
ASDDA1<-c("normal","over","low","over","normal","over","low","over","normal","over","DF","DF","DF","DF","DF","DF","DF","DF")
ASDDb1<-c("normal","over","low","over","normal","over","low","over","normal","over","DF","DF","DF","DF","DF","DF","DF","DF")
ASDDAf1<-c("normal","over","low","over","normal","over","low","over","normal","over","DF","DF","DF","DF","DF","DF","DF","DF")
ASDDAf11<-c("normal","over","low","over","normal","over","low","over","normal","over","DF","DF","DF","DF","DF","DF","DF","DF")
ASDDAf21<-c("normal","over","low","over","normal","over","low","over","normal","over","DF","DF","DF","DF","DF","DF","DF","DF")
ASDDAf31<-c("normal","over","low","over","normal","over","low","over","normal","over","DF","DF","DF","DF","DF","DF","DF","DF")
ASDDAf41<-c("normal","over","low","over","normal","over","low","over","normal","over","DF","DF","DF","DF","DF","DF","DF","DF")
ASDDAf51<-c("normal","over","low","over","normal","over","low","over","normal","over","DF","DF","DF","DF","DF","DF","DF","DF")
Gene_states22 <- data.frame(gene_symbol, panel, ASDDA, ASDDb, ASDDAf, ASDDAf1, ASDDAf2, 
                            ASDDAf3, ASDDAf4, ASDDAf5, ASDDA1, ASDDb1, ASDDAf1, ASDDAf11,
                            ASDDAf21, ASDDAf31, ASDDAf41, ASDDAf51)

然后我创建了6个热图:

library(plotly)
library(ggplot2); library(reshape2)
HG3 <- split(Gene_states22[,1:15], Gene_states22$panel)
HG4 <- melt(HG3, id.vars= c("gene_symbol","panel"))
HG4 <- HG4[,-5]
HG5 <- split(HG4, HG4$panel)
p <- list()
for(i in 1:as.numeric(length(HG5))){
  p[[i]] <- ggplotly(
    ggplot(HG5[[i]], 
           aes(gene_symbol,variable)) + 
      geom_tile(aes(fill = value),
                colour = "grey50") + 
      scale_fill_manual(values=c("white", "red", "blue","black","yellow","green","brown"))+
      labs(title = "Heatmap", subtitle = names(HG5[i]), 
           x = "gene_symbol", y = "sample", fill = "value")+
      guides(fill=FALSE)+
      theme(title = element_text(family = "sans serif", size = 14, face = "bold"), 
            axis.title = element_text(family = "sans serif", size = 16, 
                                      face = "bold", color = "black"), 
            axis.text.x = element_text(family = "sans serif", size = 11),
            axis.text.y= element_text(family = "sans serif", size = 11),
            axis.title.y = element_text(vjust = 10,hjust = 10),
            panel.background = element_rect(fill = NA),
            panel.grid.major = element_line(colour = "grey50"),
            panel.spacing = unit(0, "lines"),
            strip.placement = "outside")
  )%>%
    layout(autosize = F, width=1350, height=600,
           hoverlabel = list(bgcolor = "white",
                             font = list(family = "sans serif", size = 9, color = "black"))) 
}

然后我将它们与subplot()组合成一个

subplot(p) %>% 
  layout(yaxis = list(title = "sample"), 
         xaxis=list(title=names(HG5[1])),
         margin = list(l = 200, b = -10, t =-10), 
         xaxis2 = list(title = names(HG5[2])), 
         xaxis3 = list(title = names(HG5[3])), 
         xaxis4 = list(title = names(HG5[4])),
         xaxis5 = list(title = names(HG5[5])),
         xaxis6 = list(title = names(HG5[6])))

正如下面的图片所示,当我创建6个热图时,panel的不同类型可以正确显示绘图。

enter image description here

问题是panel类型的数量可能从1到8不等,因此我想在默认情况下将8个轴标题赋予子图。但是当我通过以下方式添加其他标题时:

subplot(p)%>% 
  layout(yaxis = list(title = "sample"),
         xaxis=list(title=names(HG5[1])),
         margin = list(l = 200, b = -10, t =-10), 
         xaxis2 = list(title = names(HG5[2])), 
         xaxis3 = list(title = names(HG5[3])), 
         xaxis4 = list(title = names(HG5[4])),
         xaxis5 = list(title = names(HG5[5])),
         xaxis6 = list(title = names(HG5[6])),
         xaxis7 = list(title = names(HG5[7])))

子图未正确显示,就像另一个图卡在原始图上一样。 enter image description here

我无法弄清楚为什么会发生这种情况,当然我会选择其他解决方案,从中选择有关xaxes标题的方法。

1 个答案:

答案 0 :(得分:1)

根据我的评论,如果您希望基于一个(或两个)变量的值拆分数据帧,并将每个切片绘制为单独的图,则刻面是实现外观的一种常见方法。

查看以下代码是否适合您。我跳过了问题示例中的某些主题规范,因为它们对问题并不重要:

pp <- ggplot(HG4, 
       aes(gene_symbol,variable)) + 
  geom_tile(aes(fill = value),
            colour = "grey50") + 
  facet_grid(~panel, scales = "free") +

  # it looks like you have more colours than values here.
  # I recommend using a named vector in this case, so that 
  # the same value always maps to the same colour.
  # e.g. values = c("DF" = "white", "low" = "red", ...)
  scale_fill_manual(values = c("white", "red", "blue", "black", "yellow", "green", "brown")) +

  labs(title = "Heatmap", x = "gene_symbol", y = "sample", fill = "value") +
  guides(fill = FALSE)+
  theme(panel.background = element_rect(fill = NA),
        panel.spacing = unit(0, "lines"),
        strip.placement = "outside")

ggplotly(pp,
         width = 1350, height = 600) %>%
  # note: specifying width / height in layout() has been deprecated
  # in recent versions of plotly. when I used it that way, I got
  # a warning to specify it within ggplotly() instead.
  layout(autosize = F, 
         hoverlabel = list(bgcolor = "white",
                           font = list(family = "sans serif", size = 9, color = "black"))) 

plot