欢迎光临散文网 会员登陆 & 注册

SAS绘图、R绘图哪个更适合你? (二):R绘图鉴赏

2023-08-19 17:52 作者:统计_碎碎念  | 我要投稿

  前面我们一起鉴赏了SAS的绘图本领,今天我们来领略R的绘图风采,两者各有千秋!后续将会一一更新绘图教程,包括图中的每个细节调整,关键的Code讲解,致力于共同学习进步,打造高质量图表!

  下期我们将会就R和SAS两款统计分析软件的绘图功能进行对比,从绘图对于科研的重要性到绘图的原理,以及如何才算学会了绘图这门技巧进行详细讲解。


1.1 相关图

热图

pvalue <- matrix(ifelse(dfP <= 0.001, "***", 

          ifelse(dfP <= 0.01, "**",                               

ifelse(dfP <= 0.05, "*", " "))), nrow = nrow(dfspearman))

setwd("E:/DATA/TP/20230511-4500/FIG")

png("Heatmap4.png",units="in", width=10, height=8,res=400)

pheatmap(  

dfspearman, 

angle_col = 45,  

color = colorRampPalette(c("#3C5488", "#FFFFFF", "#E64B35"))(50),  

show_rownames = T, 

display_numbers =pvalue,number_color="black",number_format="%.2e", fontsize_number = 16, 

col_names_side = "top",  

show_colnames =T,  

fontfamily= "myFont",  

border = F,  

# cluster_rows = FALSE,  

 cluster_cols  = FALSE,  

fontsize = 16,  

fontsize_row=16,  

fontsize_col=16

)

dev.off()


网络图和弦图


library(showtext)

showtext_auto(enable = T)

nodegroup<-as.data.frame(cbind(Id=unique(c(df$x,df$y))))

#write.table(nodegroup,'node1.txt',row.names = F,sep = '\t',quote = F,fileEncoding = 'GB2312')

node1<-c('HRDB'=  '#fb0d1c',

'VR'=  '#b4701d','

2s'=  '#c56cfb',

'pc'=  '#c56cfb',

'pNd'=  '#c56cfb',

'24e'=  '#44fffc',

'rMf'=  '#44fffc',

'rMSg'=  '#44fffc',

'2s'  ='#f8bf6e',

'Hk'=  '#f8bf6e',

'Ho'  ='#f8bf6e',

'24-LF'=  '#fc665d',

'LF'=  '#fc665d',

'L'  ='#fc665d',

'24-LF/HF'=  '#77d07a',

'LF/'  ='#77d07a',

'24-SDNN'  ='#77d07a',

'SDN'  ='#78c7ec',

'SDNN'  ='#78c7ec')

nodegroup$color1<-node1[nodegroup$Id]

pdf('circos2.pdf',width = 5,height = 6)

circos.clear()

circos.par("track.height" = 0.3,    gap.after=c(5,5,rep(1,2),5,rep(1,2),5,rep(1,2),5,rep(1,2),5,rep(1,2),5,rep(1,1),5),

 start.degree=90)

circos.initialize(factors =nodegroup$Id,xlim = c(0,1))

length(get.all.sector.index())

circos.track(ylim = c(0,1),bg.border=NA)

circos.trackPlotRegion(  

track.index = 2,  

ylim = c(0, 1),   

track.height = 0.06, bg.border = 'black', bg.col =node1,

  panel.fun = function(x, y) {

sector.index = get.cell.meta.data('sector.index')

xlim = get.cell.meta.data('xlim')

ylim = get.cell.meta.data('ylim')    

circos.text(CELL_META$xcenter, 

CELL_META$cell.ylim[2]+1 ,                

CELL_META$sector.index,                

facing = "reverse.clockwise",                 

adj = c(1,0),               

 niceFacing = TRUE,               

 cex = 0.6)  

} )

nodecolor<-nodegroup$color1

names(nodecolor)<-nodegroup$Id

col_meth<- colorRamp2(c(0.1, 0.5, 0.9), c('#0b24fb','#feb7c2', '#fc0d1b'))

for(i in 1:nrow(df)){  

circos.link(df$x[i], 0.5, df$y[i], 0.5,              

lwd=df$r[i]*2,              

col = col_meth(df$r[i]))

}

library(ComplexHeatmap)

lgd_links = Legend(at = c(0.1, 0.5, 0.9), col_fun = col_meth,                    title_position = "topleft",                   

title='Correlation')

lgd_list_vertical = packLegend(lgd_links)

draw(lgd_list_vertical, x = unit(4, "mm"), y = unit(4, "mm"), just = c("left", "bottom"))

dev.off()

基因互作图


1.2 聚类图

复杂热图


groupcolor <- c("#2878B5","#C82423")

names(groupcolor) <- c("pre","post") #color1

agecolor <- c("#008000","#3cb371","#84c09c","#5e8e7e")

names(agecolor) <- c("1","2","3","4") #color2

contentcolor <-c("#8ECFC9","#FFBE7A","#FA7F6F",'#82B0D2')

names(contentcolor) <- c("Th1","Th2","Th17/Th22","Multi") #color3

ann_colors <- list(Group=groupcolor, Content= contentcolor,Age=agecolor) #Color Summary List####heatmap####

library(pheatmap)windowsFonts(myFont = windowsFont("Times New Roman"),             KT = windowsFont("楷体"),             

ST = windowsFont("宋体")) 

setwd("E:/DATA/TP/20230511-4500/FIG")

png("Heatmap3.png",units="in", width=10, height=8,res=400)

pheatmap(  df,  color = colorRampPalette(c("#3C5488", "#FFFFFF", "#E64B35"))(50), 

show_rownames = T,  

show_colnames =F,  

annotation_row = dfgroup1,  

annotation_col = dfgroup2,  

fontfamily= "myFont",  

annotation_colors = ann_colors,  

border = F,

   cluster_rows = FALSE,

   cluster_cols  = FALSE,

  fontsize = 16,

  fontsize_row=16,

  fontsize_col=16

)

dev.off()


PCA可视化图



冷热点聚集性分析地图

1.3 展示模型中变量效应量的图

机器学习中特征度可视化


条件过程分析效应图

森林图

点线图

非线性模型

风险分数图

列线图

1.4 数据挖掘展示图

词云图

堆积图

箱线图

散点图

气泡图

1.5 机器学习

模型性能验证折线图

验证集区分度测试图

“黑盒”模型重要特征可视化



校正曲线与DCA曲线

  setwd("D:/DATA/TP/20230527-COX-LASSO/FIG")

  png("DCA2.png",units="in", width=6, height=6,res=400)

dca(Surv(Survival_time, Death) ~ Nomogram1,

                         data = df,

                         time =90,

                        thresholds = 1:100/ 100) %>%

  plot(smooth = T,lwd = 1.2)+theme_classic()+

# theme_prism(base_size =17)+

    theme(legend.position="top")+

    scale_x_continuous(

      limits = c(0, 0.85),

      guide = "prism_minor")+

 scale_colour_manual(values=c("blue","red","green","orange","grey"))+ 

theme(axis.text.x = element_text(size=25,face="bold",colour = "black",family="A"))+   

theme(axis.text.y = element_text(size=25,face = "bold",colour = "black",family = "A"))+    

theme(axis.title.x = element_text(size=25,face="bold",colour = "black",family="A"))+    

theme(axis.title.y = element_text(size=25,face = "bold",colour = "black",family = "A"))

dev.off()

setwd("D:/DATA/TP/20230527-COX-LASSO/FIG")

png("DCA2.png",units="in", width=6, height=6,res=400)

dca(Surv(Survival_time, Death) ~ Nomogram0,

    data = df,

time =30,

thresholds = 1:100/ 100) %>%  

plot(smooth = T,lwd = 1.2)+theme_classic()+ 

 # theme_prism(base_size =17)+  

theme(legend.position="top")+  

scale_x_continuous(

    limits = c(0, 0.85),

    guide = "prism_minor")+

scale_colour_manual(values= c("blue","red","green","orange","grey"))+  theme(axis.text.x = element_text(size=25,face="bold",colour = "black",family="A"))+  

theme(axis.text.y = element_text(size=25,face = "bold",colour = "black",family = "A"))+  

theme(axis.title.x = element_text(size=25,face="bold",colour = "black",family="A"))+  

theme(axis.title.y = element_text(size=25,face = "bold",colour = "black",family = "A"))

dev.off()


常用科研

# 暖色调:#FF5733, #FFC300, #FFA012, #FF7F50, #FFB6C1

# 冷色调:#87CEFA, #00BFFF, #1E90FF, #6495ED, #ADD8E6

# 土色调:#8B4513, #A0522D, #D2691E, #CD853F, #F4A460

# 棕色调:#8B6B23, #BC8F8F, #DEB887, #D2B48C, #F5DEB3

# 紫色调:#9400D3, #8A2BE2, #9370DB, #BA55D3, #DA70D6

# 黄色调:#FFD700, #FFFF00, #F0E68C, #FFC107, #FFA500

# 绿色调:#00FF00, #32CD32, #3CB371, #2E8B57, #228B22

# 灰色调:#808080, #A9A9A9, #C0C0C0, #D3D3D3, #E6E6E6

关注微信公众号,获取更多相关内容

以上绘图教程后续更新,如急需代码,请后台私信联系


SAS绘图、R绘图哪个更适合你? (二):R绘图鉴赏的评论 (共 条)

分享到微博请遵守国家法律