R语言相关性分析

Python020

R语言相关性分析,第1张

1.  R语言自带函数cor(data, method=" ")可以快速计算出相关系数 ,数据类型:data.frame

 如data.frame为:zz, 绘图如下:

a. single protein:线性回归画法

1. ggplot(zz,aes(x=a, y=HDL))+

   geom_point(alpha=1,colour="#FFA54F")+

   geom_smooth(method = lm,colour="#8B658B")+

   #scale_color_brewer(palette = "Set1")+

   theme_bw()+

   labs(x="Ferritin",y="HDL.C",title="Pearson’s correlation test of ferritin and HDL.C")+

   annotate("text", x = 1000, y = 2.5, label = "r = -0.51",colour="black",size=4)

2. library(ggstatsplot)

 ggscatterstats(data = alldata,

               y = TRANSFUSION.UNIT,

                x = NPTXR,

                centrality.para = "mean",  #"mean" or "median"                         

               margins = "both",                                       

                xfill = "#D8BFD8",

                yfill = "#EEDD82",

                #line.size= ,

                line.color="#8B6969",

               point.color="#2F4F4F",

                marginal.size=4,

               marginal.type = "density", # "histogram", "boxplot", "density", "violin", "densigram")

                title = "Relationship between TRANSFUSION.UNIT and NPTXR")

b. ggcorrplot, 全部蛋白 global correlation map 画法

ggcorrplot(cor(alldata))

2.  summary(lm(y~x),method=" ") %>%.[["coefficients"]]   正规线性回归

     (其实就是:a<-lm(y~x1+x2+...,data)

      plot(summary(lm(y~x),method=" ")) #绘图

3.  ggcor部分数据绘图:  数据类型为data.frame,纵坐标为各指标or各蛋白,行为观测值。

data <- fortify_cor(alldata[,10:11],alldata,cluster.type = "col")

ggcor<-ggcor(data,label_size=0.5) +

  geom_colour()+

  theme(axis.text.x = element_text(colour = "black",size = 4.7),

                                                        axis.text.y=element_text(size=5.5),

                                                        axis.ticks=element_blank())+

  geom_num(aes(num=r),colour="black",size=1.5)

4. corrr包画法

datasets::mtcars %>%

  correlate() %>%

  focus(-cyl, -vs, mirror = TRUE) %>%

  rearrange() %>%

  network_plot(min_cor = .2)

给你一些代码,你慢慢研究:

install.packages('ggplot2')

library(ggplot2)

ggplot(a)+geom_bar(aes(x1,y,fill/col=x1/x2),position='dodge',stat='summary',fun='sum'/'mean')条形图+theme(text = element_text(family='Kai'))

ggplot(a)+geom_boxplot(aes(x1,y,col=x1/x2))箱线图

ggplot(a)+geom_point(aes(x1,y,col=x1/x2),position=position_jitter(width=0.04))散点图

1+geom_point(aes(x1,y,col=x1/x2),stat='summary',fun='sum'/'mean')+散点

2+geom_line(aes(x1,y,group=1/x2,col=x1/x2),stat='summary',fun='sum'/'mean')+折线

3+geom_errorbar(aes(x=x1,ymin=y-se,ymax=y+se,col=x1/x2),position=position_dodge(0.9),width=0.2)+误差

4+geom_text(aes(x1,y,label=marker,col=x1/x2),position=position_dodge(0.9)vjust=2或y+2)+显著字母

ggplot(a,aes(x1,y,fill/col=x1/x2))+geom_bar(position='dodge',stat='summary',fun='sum'/'mean')+geom_errorbar(aes(ymin=y-se,ymax=y+se),position=position_dodge(0.9),width=0.2)+geom_text(aes(label=marker),position=position_dodge(0.9),vjust=-2)条形图+误差棒+显著字母(坐标写一次即可)

ggplot(a,aes(x1,y,col=x1/x2))+geom_point(position=position_jitter(width=0.04),stat='summary',fun='sum'/'mean')+geom_line(aes(group=1/x2),stat='summary',fun='sum'/'mean')+geom_errorbar(aes(ymin=y-se,ymax=y+se),position=position_dodge(0.9),width=0.2)+geom_text(aes(label=marker),position=position_dodge(0.9),vjust=-2)散点图+折线+误差棒+显著字母(坐标写一次即可)

+geom_density(aes(y=liqi))密度图(1个数值型)

+geom_area(aes(x=tan,y=liqi))区域图(2个数值型)

+geom_smooth(aes(x=tan,y=liqi,group/col=chong),formula=y~x,method='lm',se=F)拟合图,分组/线条颜色(2个数值型)

+facet_wrap(~riqi,ncol/nrow=2,labeller='label_both/value')分面图,每行或每列分面数,分面标题

+xlab('自变量1(单位)')+ylab('因变量(单位)')+scale_fill_discrete(name='自变量2')更改轴和图例名称+coord_cartesian(ylim= c(0,80))限定轴范围

(fill=x1/x2,有此即可变色)+scale_fill_manual(values = c('grey70', 'grey50', 'grey30'))改变条形填充颜色(颜色数量=分组数量)

(col=x1/x2,有此即可变色)+scale_color_manual(values = c('red', 'orange', 'yellow'))改变颜色(颜色数量=分组数量)

1、利用geom_smooth进行曲线的拟合。

2、利用spline进行插值操作。R语言,一种自由软件编程语言与操作环境,主要用于统计分析、绘图、数据挖掘。R主要是以命令行操作,同时有人开发了几种图形用户界面。