在讲交互式热图之前先来讲讲小鹿最近遇到的头疼事儿,想来大多数没有用过云工具绘图的小伙伴们应该都遇到过此类烦恼,大家是否在精修一张图的时候反复测试各种参数?比如热图?
小鹿在绘制热图的时候总是头疼这些参数,比如热图的色阶一定要设置的既保证可以清晰的展示数据的分类,又要保持一定的美观度;
再比如热图的宽高比例修改,需要每次都反复调试找到维持保证热图全部展示又不怪异的比例;
为了避免一直在修改个别参数,作好生枯燥又机械的重复出图过程...为此,小鹿特地研究了一种新方法去完美的解决此类问题。
只需要小鹿粉们滑动滑动鼠标,输入对应的参数,就可以实现交互式绘图的过程。是不是满心期待又充满迷惑?话不多说,小鹿这就带大家探索其中的奥妙~
通过了解交互式绘图的使用过程,想必大家更想要知道这是怎么用代码实现?下面就是小鹿的神秘代码大礼包,快来看看吧!
1
载入R包
在进行R图绘制前需要载入所需R包,没有安装过请先安装~
library(colourpicker)
library(openxlsx)
library(dplyr)
library(pheatmap)
library(shinydashboard)
library(shinythemes)
library(shiny)
2
设置控件ui
大家看到上述使用过程中有很多可操作的按钮,滑动条等,其实就是R包shiny提供的控件,本文仅展示需要的控件。
以下是小鹿如何设置热图ui控件的代码:
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose file(data):",
multiple = FALSE,
accept = c("csv",".xlsx","xls","txt")), #设置输入文件的控件
fileInput("file2", "Choose file(sample):",
multiple = FALSE,
accept = c("csv",".xlsx","xls","txt")),
selectInput("select_scale", "select_scale",
c("row" = "row",
"column" = "column",
"none" = "none")), #设置选择下拉框的控件
checkboxInput("cluster_rows", "cluster_rows", FALSE), #设置是否选择的控件
checkboxInput("cluster_cols", "cluster_cols", FALSE),
colourInput("color1", "Select colour Low", "#0464bc"), #设置选择颜色的控件
colourInput("color2", "Select colour Middle", "white"),
colourInput("color3", "Select colour High", "#c81215"),
checkboxInput("show_rownames", "show_rownames", FALSE),
checkboxInput("show_colnames", "show_colnames", FALSE),
sliderInput(inputId = "cellwidth",label = "cellwidth",min = 0.5,max = 30,value = 5), #设置滑动条的控件
sliderInput(inputId = "cellheight",label = "cellheight",min = 0.5,max = 30,value = 5),
sliderInput(inputId = "fontsize_row",label = "fontsize_row",min = 0.5,max = 30,value = 5),
sliderInput(inputId = "fontsize_col",label = "fontsize_col",min = 0.5,max = 30,value = 5)
),
mainPanel(
tabsetPanel(
plotOutput("plot_heatmap",width = "1000px",height = "1000px") #设置交互式绘制热图
)
)
)
)
3
布置服务server
通过在控件中的输入以及参数设置,绘制热图进行展示。
server <- function(input, output) {
output$plot_heatmap <- renderPlot({
req(input$file1$datapath)
req(input$file2$datapath)
df <- read.xlsx(xlsxFile = input$file1$datapath,sheet = 1,colNames = TRUE,rowNames = TRUE)
sampledata <- read.xlsx(xlsxFile = input$file2$datapath,sheet = 1,colNames = TRUE,rowNames = TRUE)
color <- c(input$color1,input$color2,input$color3)
df[is.na(df)] <- apply(df,2,function(x){min(na.omit(x))})%>%na.omit()%>%min()
col <- rep(c("#1660A7","#FF6A00","#219418","#CD0C18","#814BB2","#794339"),6)
samplecolor <- list()
samplecolor$Group <- col[1:length(unique(sampledata$Group))]
names(samplecolor$Group) <- unique(sampledata$Group)
pheatmap(df,scale=input$select_scale,
cluster_rows = input$cluster_rows,cluster_cols = input$cluster_cols,
color = colorRampPalette(color)(1000),
clustering_method = "complete",
display_numbers = FALSE,number_format ="%.3f",
show_colnames = input$show_colnames,show_rownames = input$show_rownames,
border_color = NA,fontfamily= "ArialMT",
cellwidth = input$cellwidth,cellheight = input$cellheight,
fontsize_col = input$fontsize_col,fontsize_row = input$fontsize_row,
annotation_col = sampledata,
annotation_colors = samplecolor
)
})
}
4
Run生成云工具
shinyApp(ui = ui, server = server)
简单四步即可实现云工具绘制交互式热图,轻松解决调参难题;
省时省力还省心,是不是感觉像发现了新大陆?
大家若是感兴趣可以了解一下shiny的官方网站,里面不仅展示了很多适合小白学习的易理解的简易版交互式工具,还有一些大佬写的高级处理云工具以供开拓眼界及膜拜...
官方链接:
https://shiny.rstudio.com/
今天的交互式热图的学习就到这里了,但是交互式工具的学习还远没有结束,小鹿会持续为大家带来不一样的惊喜,期待下次再见吧~
小鹿版本的交互式热图云工具的链接就放在本文下方,如有感兴趣的小伙伴请点击此链接,带上你们的数据去感受不一样的绘图体验吧~如有问题,欢迎留言~
https://wangcy0501.shinyapps.io/myheatmap/
文末看点 鹿明生物
上海鹿明生物科技有限公司多年来,一直专注于生命科学和生命技术领域,是国内早期开展以蛋白组和代谢组为基础的多层组学整合实验与分析的团队。
如果你还在为不知如何绘制SCI图表而忧愁,还在为毕业论文制图效率低下美观度不满意而焦心,或者想要找到一个高效制图的集中地……
欧易云是欧易/鹿明生物旗下的科研图表聚集地,里面火热绘图工具+教程,使用便捷且能自动调色增加科研图的美观度,同时还将上线更多科研图功能,请持续关注欧易云平台,相信聪明的你一定不会错过!
热度翻倍,工具直达,只等你来云平台地址:
https://cloud.oebiotech.cn/task/
猜你还想看
END
森淼 撰文
欢迎转发到朋友圈
本文系鹿明生物原创
转载请注明本文转自鹿明生物
我知道你在看哟
点“阅读原文”了解更多