美文网首页
多组散点图+配对连线+差异分析

多组散点图+配对连线+差异分析

作者: Z_bioinfo | 来源:发表于2022-08-08 21:17 被阅读0次
# 生成示例数据
dt = data.frame(sample = paste0('sample',1:30),
                A = runif(30,0,450),
                B = runif(30,100,500),
                C = runif(30,0,300))
> head(dt)
   sample         A        B         C
1 sample1 197.94364 161.0455 223.46878
2 sample2  58.76647 340.5557 164.26801
3 sample3 322.71984 360.9137  88.98755
4 sample4 195.32401 185.9487 213.12908
5 sample5 388.37149 420.9100  40.88598
6 sample6  28.39112 208.1737 236.12682

# 长宽转换
library(tidyverse)
library(reshape2)

dt_long <- melt(dt,
                measure.vars = c("A","B",'C'), 
                variable.name = "group",
                value.name = "value")
#measure.vars:度量变量(我们想要放进同一列的变量)
#variable.name:为新列取名,如果不取名,默认新增列的列名就是“variable”
#value.name:新列对应值所在的变量名
head(dt_long)
   sample group     value
1 sample1     A 197.94364
2 sample2     A  58.76647
3 sample3     A 322.71984
4 sample4     A 195.32401
5 sample5     A 388.37149
6 sample6     A  28.39112

代码解析

runif()R语言中的函数用于创建均匀分布的随机偏差。
用法: runif(n, min, max)
n:表示观察次数
min, max:表示分布的下限和上限

绘图

# 绘图
library(ggplot2)
library(ggthemes)

# 绘制散点图+配对连线
p1 <- ggplot(dt_long,aes(group, value, fill = group))+
  geom_line(aes(group = sample),
            size = 0.5)+#图层在下,就不会显示到圆心的连线
  geom_point(shape = 21,
             size = 3,
             stroke = 0.6,
             color = 'black')+
  scale_x_discrete(expand = c(-1.05, 0)) + # 坐标轴起始
  scale_fill_manual(values = c('#800040','#fc6666','#108080')) + #设置自己喜欢的颜色
  geom_rangeframe() + # 坐标轴分离
  theme_tufte() +
  theme(legend.position = 'none',  # 标签字体等
        axis.text.y = element_text(size = 14,
                                   face = "bold"),
        axis.text.x = element_text(size =14,
                                   face = "bold"),
        axis.title.y = element_text(size = 15, 
                                    color = "black",
                                    face = "bold")) +
  labs(x = ' ', 
       y = 'Values')

p1
image.png
# 为了绘制原图的差异形式 手动计算p值,这里用的t检验
library(rstatix)
result = t_test(dt_long,value~group)  
head(result)
# A tibble: 3 x 10
  .y.   group1 group2    n1    n2 statistic    df         p     p.adj p.adj.signif
  <chr> <chr>  <chr>  <int> <int>     <dbl> <dbl>     <dbl>     <dbl> <chr>       
1 value A      B         30    30     -2.16  56.1 0.035     0.07      ns          
2 value A      C         30    30      1.52  44.7 0.135     0.135     ns          
3 value B      C         30    30      4.58  49.9 0.0000317 0.0000951 ****

** 添加显著性标记**

p2 <- p1 +  
  coord_cartesian(clip = 'off',ylim = c(0,500))+ 
  theme(plot.margin = margin(1,0,0,0.5,'cm')) + #设置编剧
  annotate('segment',x=1,xend=1.99,y=510,yend=510,color='black',cex=.6)+
  annotate('segment',x=1,xend=1,y=505,yend=515,color='black',cex=.6)+
  annotate('segment',x=1.99,xend=1.99,y=505,yend=515,color='black',cex=.6)+
  annotate('segment',x=2.01,xend=3,y=510,yend=510,color='black',cex=.6)+
  annotate('segment',x=2.01,xend=2.01,y=505,yend=515,color='black',cex=.6)+
  annotate('segment',x=3,xend=3,y=505,yend=515,color='black',cex=.6)+
  annotate('segment',x=1,xend=3,y=530,yend=530,color='black',cex=.6)+
  annotate('segment',x=1,xend=1,y=525,yend=535,color='black',cex=.6)+
  annotate('segment',x=3,xend=3,y=525,yend=535,color='black',cex=.6)+
  annotate("text", x = 1.5, y = 520, label ="p = 0.080",size = 6)+
  annotate("text", x = 2.5, y = 520, label ="p = 0.129",size = 6)+
  annotate("text", x = 2, y = 540, label ="p = 0.0004",size = 6)
p2
#保存图片
ggsave('paired_scatter.pdf',p2,width = 8,height = 7)

代码解析

coord_cartesian(clip = "off") ,如果加上这一行命令,后面的图置于最上层,不加的效果是显示不全
image.png

相关文章

网友评论

      本文标题:多组散点图+配对连线+差异分析

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