使用plot_ly()将ggplot2热图转换为相对的plotly热图

时间:2018-06-15 16:21:40

标签: r ggplot2 plotly

您好我有以下数据集:

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)

我用:

创建了一个ggplot2热图
library(plotly)
library(ggplot2); library(reshape2)
g <- melt(Gene_states22, id.vars = c("gene_symbol","panel"))
p1<-ggplot(g, aes(gene_symbol,variable)) + geom_tile(aes(fill = value),
                                                     colour = "grey50") + scale_fill_manual(values=c("white", "red", "blue","black","yellow","green","brown"))+
  facet_grid(~panel,switch = "x",scales ="free_x")+
  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 = 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")
p1

然后我用ggplotly()

将其转换为情节
ggplotly(p1)%>%
  layout( autosize = F, width = 1350, height = 600,hoverlabel = list(bgcolor = "white",
                                                                     font = list(family = "sans serif", 
                                                                                 size = 9, 
                                                                                 color = "black")))

问题是ggplot2的一些功能,比如element_text(),例如调整标签与图形的距离,在图形上不能正常工作,我想用plotly和{{1}专门创建相同的热图。但我在这方面经验不足,无法达到100%。

0 个答案:

没有答案