❝本节来介绍如何R来自定义构建函数来进行数据处理及绘图,在之前展示案例的基础上进行了一些小的改动,下面通过1个案例来进行展示;
加载R包
代码语言:javascript复制library(tidyverse)
library(magrittr)
导入数据
代码语言:javascript复制df <- read.delim("data.xls",row.names = 1,sep="t")
构建数据清洗函数
代码语言:javascript复制plot_data_prep <- function(data,gene){
plot_data <- data %>%
pivot_longer(-gene) %>%
pivot_longer(names_to = "name_2", values_to = "value_2",gene) %>%
group_by(name_2,name) %>%
summarise(cor= cor.test(value_2,value,method="spearman")$estimate,
p.value = cor.test(value_2,value,method="spearman")$p.value) %>%
set_colnames(c("gene_1","gene_2","cor","pvalue")) %>%
filter(pvalue < 0.05) %>%
arrange(desc(abs(cor)))%>%
# dplyr::slice(1:500) %>%
mutate(p_signif=symnum(pvalue,corr = FALSE, na = FALSE,
cutpoints = c(0, 0.001, 0.01, 0.05, 0.1, 1),
symbols = c("***", "**", "*", ".", " ")))
return(plot_data)
}
代码语言:javascript复制❝上面我们定义了一个函数来计算某一基因与其它全部基因之间的相关性,下面我们来进行测序 ❞
gene <- "B2M"
plot_data_prep(df,gene)
代码语言:javascript复制 gene_1 gene_2 cor pvalue p_signif
<chr> <chr> <dbl> <dbl> <noquote>
1 B2M APOBEC3H 0.577 1.48e-25 ***
2 B2M XCL2 0.577 1.51e-25 ***
3 B2M KIR2DL4 0.565 2.31e-24 ***
4 B2M TIFAB 0.565 2.63e-24 ***
5 B2M XCL1 0.561 5.92e-24 ***
6 B2M FUT7 0.558 1.21e-23 ***
7 B2M ZBED2 0.557 1.57e-23 ***
8 B2M IFNG 0.526 8.71e-21 ***
9 B2M NCR3 0.524 1.39e-20 ***
10 B2M SSTR3 0.506 4.22e-19 ***
接下来我们继续定义一个绘图函数来进行数据可视化
构建绘图函数
代码语言:javascript复制make_plot <- function(data,x,y){
ggplot(data)
geom_col(aes(x={{x}},y={{y}}, fill = {{x}} > 0),
size = 0.25, color = "white")
geom_point(aes(x={{x}},y={{y}},
color=ifelse({{x}} > 0,"#BA7A70","#829BAB")),size=4.1)
geom_text(aes(x = ifelse({{x}} > 0, -.005, .005),y = {{y}},
label = gene_2,
color=ifelse({{x}} > 0,"#BA7A70","#829BAB"),
hjust = ifelse({{x}} > 0, 1, 0)),size = 3.8)
geom_vline(xintercept=0,size=1,color="grey40")
scale_y_discrete(expand = c(.025,.025))
scale_fill_manual(values = c("TRUE" = "#BA7A70","FALSE" = "#829BAB"))
scale_color_manual(values = c("#829BAB","#BA7A70"))
coord_cartesian(clip = "off")
theme_minimal()
theme(panel.grid = element_blank(),
plot.background = element_rect(fill="Aliceblue",color="Aliceblue"),
axis.text.y = element_blank(),
axis.title = element_blank(),
legend.position = "none",
axis.text.x = element_text(face = "bold", size =rel(1), color = "black"))
}
构建好绘图函数后让我们来进行可视化的操作,由于原始数据较多在此我们筛选一部分数据进行可视化操作
数据筛选
代码语言:javascript复制p <- plot_data_prep(df,gene) %>% select(1,2,3,5) %>% sample_frac(.1) %>%
arrange(cor)
p$gene_2 <- factor(p$gene_2,levels = p$gene_2)
数据可视化
代码语言:javascript复制make_plot(p,cor,gene_2)