将某些图与子图组合后,“填充”标签无法正常工作

时间:2018-06-19 21:01:17

标签: 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","small","DF","DF","DF","DF","DF","DF","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)

然后我根据diffrenet面板类型的数量创建4个热图。

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()

p[[1]]<-ggplotly(
  ggplot(HG5[[1]], 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",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,hoverlabel = list(bgcolor = "white",
                                         font = list(family = "sans serif", 
                                                     size = 9, 
                                                     color = "black"))) 
for(i in 2:as.numeric(length(HG5)-1)){


  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",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.title.y = element_blank(),
            axis.text.y=element_blank(),
            panel.background = element_rect(fill = NA),
            panel.grid.major = element_line(colour = "grey50"),
            panel.spacing = unit(0, "lines"),
            strip.placement = "outside")
  )%>%
    layout( autosize = F,hoverlabel = list(bgcolor = "white",
                                           font = list(family = "sans serif", 
                                                       size = 9, 
                                                       color = "black")))
}
for(i in as.numeric(length(HG5):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",x = "gene_symbol",y="sample",fill="value")+
      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 = 10),
            axis.title.y = element_blank(),
            axis.text.y=element_blank(),
            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(p)%>% layout(yaxis = list(title = "sample"),xaxis=list(title="gene_symbol"),margin = list(l = 200, b = -10, t =-10))

问题是我希望在最后一个绘图之后仅显示一次值标签,但是正如您在图片中看到的那样,它以一种非常奇怪的方式显示值。通常,它应仅显示“低”,“正常”,“上方”,“ DF”。我想做的是使用guides(fill=FALSE)隐藏前三个热图的值标签,并仅显示最后一个热图值标签。 enter image description here

1 个答案:

答案 0 :(得分:2)

它发生在密谋上。它带回了传说。不简单,但是您可以通过以下方法摆脱传奇:

for (i in 1:39){
  p[[2]]$x$data[[i]]$showlegend <- FALSE
}


for (i in 1:39){
  p[[3]]$x$data[[i]]$showlegend <- FALSE
}

for (i in 1:39){
  p[[4]]$x$data[[i]]$showlegend <- FALSE
}

subplot(p) %>% layout(yaxis = list(title = "sample"),xaxis=list(title="gene_symbol"),margin = list(l = 200, b = -10, t =-10))

enter image description here