美文网首页
绘图专题(2):一般统计图表

绘图专题(2):一般统计图表

作者: 挽山 | 来源:发表于2020-02-28 15:21 被阅读0次

(一)半圆条形图 (R)

type<-c('DEGs','DEMs','DELs','OTHER')
percent<-c(40,20,8,32)

d<-data.frame(type,percent)
cols<-rainbow(length(type))

library(RColorBrewer)
cols1<-brewer.pal(12,'Paired')
cols2<-brewer.pal(12,'Set3')
cols<-c(cols1,cols2)

f<-function(name,value){
  xsize<-200
  plot(0,0,xlab = '',ylab = '',axes = F, xlim = c(-xsize, xsize), ylim = c(-xsize, xsize))
  for (i in 1:length(name)) {
    info=name[i]
    percent=value[i]
    k=1:percent/100
    r=xsize*(length(name)-i+1)/length(name)
    x=r*sin(k*2*pi); y=r*cos(k*2*pi)
    #添加标签
    text(-18,r,info,pos=2,cex=3,col=cols[i])
    text(-9,r,paste(percent,'%'),cex=3,col=cols[i])
    #设置线条粗细
    lines(x,y,col=cols[i],lwd=(length(name)-i+1)*5)
  }
}
f(type,percent)
dev.off

#配色模板
#install.packages("ggsci")
library("ggsci")

带星号条形图(graphpad里画的挺好的、还有连线的箱式图)

Normal <- c(0.83, 0.79, 0.99, 0.69)
Cancer <- c(0.56, 0.56, 0.64, 0.52)
m <- c(mean(Normal), mean(Cancer))
s <- c(sd(Normal), sd(Cancer))
d <- data.frame(V = c("Normal", "Cancer"), mean = m, sd = s)
d$V <- factor(d$V, levels = c("Normal", "Cancer"))

library(ggplot2)

p <- ggplot(d, aes(V, mean, fill = V, width = 0.5))
p <- p + 
  geom_errorbar(aes(ymin = mean, ymax = mean + sd, width = 0.2),
                position = position_dodge(width = 0.8)) +
  geom_bar(stat = "identity", 
           position = position_dodge(width = 0.8),
           colour = "black") +
  scale_fill_manual(values = c("grey80", "white")) +
  theme_bw() + theme(legend.position = "none") + 
  xlab("") + 
  ylab("") +
  theme(axis.text.x = element_text(face = "bold", size = 12),
        axis.text.y = element_text(face = "bold", size = 12),
        panel.grid = element_blank()) +
  scale_y_continuous(expand = c(0, 0), limits = c(0, 1.2),breaks = seq(0, 1.2, by = 0.2)) +
  geom_segment(aes(x = 1, y = 0.98, xend = 1, yend = 1.1)) +
  geom_segment(aes(x = 2, y = 0.65, xend = 2, yend = 1.1)) +
  geom_segment(aes(x = 1, y = 1.1, xend = 1.45, yend = 1.1)) + #与源代码有差异,使得“*”在连线中间
  geom_segment(aes(x = 1.55, y = 1.1, xend = 2, yend = 1.1)) +
  annotate("text", x = 1.5, y = 1.1, label = "*")

ggsave(plot = p, filename = "2.jpeg")

(二)各种韦恩图

#Vennerable:
#1-Displays Venn and Euler diagrams for up to 9 
#2-Allows the display of area-weighted Venn diagrams

#安装 https://github.com/js229/Vennerable
biocLite(c("RBGL","graph"))
install.packages("devtools");library(devtools)
install_github("js229/Vennerable");library(Vennerable)

#帮助文档 vignette("Venn")
library(RBGL);library(graph);library(Vennerable)

#Weighted 2-set Venndiagrams
x<-c(1:100)    
y<-c(51:125)
data<-Venn(list("SetA"=x,"SetB"=y)) 
plot(data,doWeight=T) 

#Weighted 3-set Venndiagrams========================================================================================
Vcombo <- Venn(SetNames = c("CC", "PFC", "1741 DVMT"), 
               Weight = c(0, 500, 409, 604, 543, 67, 183, 146))
plot(Vcombo)

#RNA-seq vs. WGS
data<-read.table(file = file.choose(),header = T,sep = '\t')
head(data)
#CC_DEGs
a<-data[,1]
#CC_WGCNA
b<-data[,2]
#PFC_DEGs
c<-data[,3]
#PFC_WGCNA
d<-data[,4]
#WGS_2174
e<-data[,5]

vl5<-list(CC_DEGs=a,CC_WGCNA=b,WGS=c,PFC_DEGs=d,PFC_WGCNA=e)
vl3<-list(CC_WGCNA=b,WGS=c,PFC_WGCNA=e)

vv3<-Venn(vl3)
plot(vv3,type ='ChowRuskey')

#调整图vv3的注释
SetLabels<-VennGetSetLabels(vv3)
#调整其中的'setx',纵横轴位置
SetLabels[SetLabels$Label=="set","y"]=30
vv3_t<-VennSetSetLabels(vv3,SetLabels)
grid::grid.newpage()
plot(vv3_t)

#Unweighted 3-set Venndiagrams
data3<-Venn(list("CC_WGCNA"=data[,2],"WGS"=data[,5],'PFC_WGCNA'=data[,4])) 
plot(data3,
     type="ChowRuskey",
     #type="circles",
     doWeight=F,
     show =list(Faces = T, #填充色
                DarkMatter = F, #背景计算
                SetLabels = T#注释
                #FaceText = "sets", #所属集合
                #FaceText = "signature" #代码显示集合
     ) 
)

#Unweighted 4-set Venndiagrams
ellipses
##############################################################################################
source("http://www.bioconductor.org/biocLite.R")
biocLite("VennDiagram")

#帮助文档https://wenku.baidu.com/view/c3e70b2bcc22bcd126ff0cb4.html

library(VennDiagram)
library(venneuler)
library(grid)
library(futile.logger)
  
data<-read.table(file = file.choose(),header = T,sep ="\t",stringsAsFactors = F)
head(data)

mir124<-data[,1];length(mir124)#2496
mir128<-data[,1];length(mir128)#1405
mir137<-data[,1];length(mir137)#1196

setwd('E:/2019国自然/靶基因预测')
venn.diagram(x=list('miR-124'=mir124,'miR-128'=mir128,'miR-137'=mir137), 
                    filename = "Figure 1-venn.tif", 
                    imagetype = "tiff",
                    height = 1800, 
                    width = 2000, 
                    resolution=500,
                    #label.col=c(),
                    cex=0.8, #区域字号,0.5=6pt(5~8pt)
                    col="black", #边界线颜色
                    lwd=c(0.25, 0, 0), #边界线宽度(主线条0.25~1pt)
                    ext.line.lty = "dashed",  #外部线为虚线
                    fill=c("lavender","thistle","beige"), 
                    alpha=c(0.9, 0.4, 0.7), #区域透明度
                    cat.dist=c(0.21, 0.21, 0.11), #分类名称距离边的距离(可以为负数)
                    cat.pos=c(0-20, 0+20, 0-15), #对于每个圆,0是正上方。
                    cat.cex=0.6,#集合名字号
                    #cat.col=c(),
                    rotation.degree=0
                    )

#https://www.jianshu.com/p/e794d966411c (提取各部分信息)
#多维韦恩图 https://www.jianshu.com/p/285b4ac66768

(三)嵌套圆环图(python)

# -*- coding: utf-8 -*-
"""
Created on Wed Nov 28 23:18:38 2018

@author: xllix
"""
#https://blog.csdn.net/qq_41841569/article/details/83824342

import matplotlib as mpl
import matplotlib.pyplot as plt
import os

#创建工作目录
#os.mkdir('C:\\Users\\xllix\\Documents\\python')
#更改工作目录
os.chdir('C:\\Users\\xllix\\Documents\\python')
#查看工作目录
os.getcwd()

plt.figure(figsize = (10,10))

vals_out = [0.465, 0.016, 0.089, 0.429] 
vals_in= [0.494, 0.045, 0.049, 0.411] 
#vals3=[1] 

fig, ax = plt.subplots() 
labels = 'DEGs', 'DEMs', 'DELs', 'OTHERS' 
#colors = ['thistle', 'lavender', 'lightsteelblue', 'whitesmoke']
colors = ['dimgray', 'darkgray', 'lightgray', 'whitesmoke']

ax.pie(vals_out, 
       radius=1.05,
       autopct='%1.1f%%',
       pctdistance=0.9,
       startangle=190,
       colors=colors,
       #textprops={'color': 'w'}
       wedgeprops={'width': 0.3, 'edgecolor': 'w'}     
       ) 

ax.pie(vals_in, 
       radius=0.8,
       autopct='%1.1f%%',
       pctdistance=0.75,
       startangle=190,
       colors=colors,
       #textprops={'color': 'w'}
       wedgeprops={'width': 0.35, 'edgecolor': 'w'}   
       ) 

#ax.pie(vals3, radius=0.6,colors='w') 

ax.set(aspect="equal", title=' ') 

plt.legend(labels,
           bbox_to_anchor=(1, 0.8), 
           loc='best', 
           borderaxespad=0.) 
plt.show()
fig.savefig('Figure 1a.tiff', dpi=300)
print(os.getcwd())

(四)箱式图、小提琴图

#见ggpubr包专题

(五)折线图(条形图、箱式图等简单的用graphpad也挺好)

flower<-read.table(file.choose(),header=T, sep="\t")
head(flower)

by_tatal<-flower[,1]
by_dif<-flower[,2];by_dif<-by_dif[1:7]

yyn_tatal<-flower[,3]
yyn_dif<-flower[,4];yyn_dif<-yyn_dif[1:7]

png(file = "line_chart_flower_total.jpg")
plot(by_tatal,type = "o",col = "red", xlab = "hour", ylab = "flower", 
     main = "Total",ylim = c(19000,24000))
lines(yyn_tatal, type = "o", col = "blue")
dev.off()

png(file = "line_chart_flower_dif.jpg")
plot(by_dif,type = "o",col = "red", xlab = "hour", ylab = "flower", 
     main = "difference",ylim = c(0,680))
lines(yyn_dif, type = "o", col = "blue")
dev.off()

(六)桑基图

# http://mp.weixin.qq.com/s?__biz=MzA5NjU5NjQ4MA==&mid=2651165020&idx=1&sn=f37aa5d4402f859ddf5b03faba031bc5&chksm=8b5c9633bc2b1f253e60aa9b15e610c9798f521aff66ba4b8b05ae404d8829c76d2052dbff82&scene=0&xtrack=1#rd
library(ggplot2)
library(ggalluvial)
library(RColorBrewer)

# 整合
miRNA_mRNA$Freq=1#定义纵坐标,一般默认为1
miRNA_mRNA_long<- to_lodes_form(miRNA_mRNA,
                                axes = 1:2,#将miRNA和mRNA分别编号
                                id = "Cohort") #改为长数据便于画图

# geom_flow(只有中间)
ggplot(miRNA_mRNA_long,
       aes(x =factor(x,level = c("miRNA","SYMBOL")),y=Freq,stratum = stratum, alluvium = Cohort,fill = stratum, label =stratum)) +
  geom_flow( width = 1/3)+#画流动图
  geom_text(stat ="stratum" , size =3) +#添加名字
  scale_x_discrete(limits = c() )+#去掉横坐标轴
  theme_bw()+#定义主题
  theme(legend.position = "none",
        axis.title = element_blank(),
        axis.text.y= element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor  = element_blank(),
        panel.border = element_blank())+
  scale_fill_manual(values = colorRampPalette(brewer.pal(8, "Accent"))(20))#定义颜色


# geom_stratum(只有两侧)
ggplot(miRNA_mRNA_long,
       aes(x =factor(x,level = c("miRNA","SYMBOL")),y=Freq,stratum = stratum, alluvium = Cohort,fill = stratum, label =stratum)) +
  geom_stratum( width = 1/3,linetype=1,size=0.5,alpha =0.5,color = "black") +#画冲击图
  geom_text(stat ="stratum" , size =3) +#添加名字
  scale_x_discrete(limits = c() )+#去掉横坐标轴
  theme_bw()+#定义主题
  theme(legend.position = "none",
        axis.title = element_blank(),
        axis.text.y= element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor  = element_blank(),
        panel.border = element_blank())+
  scale_fill_manual(values = colorRampPalette(brewer.pal(8, "Accent"))(20))#定义颜色

# 合并绘图
ggplot(miRNA_mRNA_long,
       aes(x =factor(x,level = c("miRNA","SYMBOL")),y=Freq,stratum = stratum, alluvium = Cohort,fill = stratum, label =stratum)) +
  geom_flow( width = 1/3)+#画流动图
  geom_stratum( width = 1/3,linetype=1,size=0.5,alpha =0.5,color = "black") +#画冲击图
  geom_text(stat ="stratum" , size =3) +#添加名字
  scale_x_discrete(limits = c() )+#去掉横坐标轴
  theme_bw()+#定义主题
  theme(legend.position = "none",
        axis.title = element_blank(),
        axis.text.y= element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor  = element_blank(),
        panel.border = element_blank())+#去掉边界线
  scale_fill_manual(values = colorRampPalette(brewer.pal(8, "Accent"))(20))#定义颜色

# 合并绘图2 改变线条模式
ggplot(miRNA_mRNA_long,
       aes(x =factor(x,level = c("miRNA","SYMBOL")),y=Freq,stratum = stratum, alluvium = Cohort,fill = stratum, label =stratum)) +
  geom_flow( width = 1/3)+#画流动图
  geom_stratum( width = 1/3,linetype=0,size=0.5,alpha =0.5,color = "black") +#画冲击图
  geom_text(stat ="stratum" , size =3) +#添加名字
  scale_x_discrete(limits = c() )+#去掉横坐标轴
  theme_bw()+#定义主题
  theme(legend.position = "none",
        axis.title = element_blank(),
        axis.text.y= element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor  = element_blank(),
        panel.border = element_blank())+#去掉边界线
  scale_fill_manual(values = colorRampPalette(brewer.pal(8, "Accent"))(20))#定义颜色

图片布局 (R)---------------------------

#一、multiplot函数
install.packages("Rmisc")
library(Rmisc)
library(ggplot2)
multiplot(p1,p2,cols=1)

#二、grid.arrange函数
grid.arrange(p1,p2,nrow=2)#图摆放两行
grid.arrange(p1,p2,nrow=1)#图摆放一行
grid.arrange(p1,p2,nrow=1,ncol=2)

#三、arrangeGrop函数改变行列分布函数
#p1位于第一行并横跨两列,而arrangeGrob中的p1,p2分别分布与第二行两列。
grid.arrange(p1,arrangeGrob(p1,p2,ncol=2),nrow=2)

#layout_matrix用来设置复杂的图形布局。
grid.arrange(p1,p1,p2,ncol=2,nrow=2,layout_matrix=rbind(c(1,1),c(2,3)))

相关文章

网友评论

      本文标题:绘图专题(2):一般统计图表

      本文链接:https://www.haomeiwen.com/subject/ymklhhtx.html