如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)
在R语言的帮助文档里,apply函数的功能是:Retruns a vector or array or list of values obtained by applying a function to margins of an array or matrix.
就是说apply把一个function作用到array或者matrix的margins(可以理解为数组的每一行或者每一列)中,返回值时vector、array、list。
简单的说,apply函数经常用来计算矩阵中行或列的均值、和值的函数,具体方法如下:
定义一个3×2的矩阵:
rname = c("one","two","three")
cname = c("first","second")
b <- matrix(1:6,nrow=3, dimnames = list(rname, cname))
b为:
first second
one 1 4
two 2 5
three 3 6
继续举几个例子:
apply(b,1,sum)
上面的指令代表对矩阵b进行行计算,分别对每一行进行求和。函数涉及了三个参数:
第一个参数是指要参与计算的矩阵;
第二个参数是指按行计算还是按列计算,1——表示按行计算,2——按列计算;
第三个参数是指具体的运算参数。
上述指令的返回结果为:
one two three
5 7 9
值得注意的是,apply函数时可以针对数组进行计算你的,就是说数组未必是2维的!!
举一个3维的情况:
首先顶一个三维数组:
x=array(1:24,c(2,3,4))
三维数组为:
, , 1
[,1] [,2] [,3]
[1,]135
[2,]246
, , 2
[,1] [,2] [,3]
[1,]79 11
[2,]8 10 12
, , 3
[,1] [,2] [,3]
[1,] 13 15 17
[2,] 14 16 18
, , 4
[,1] [,2] [,3]
[1,] 19 21 23
[2,] 20 22 24
使用如下指令:
apply(x,1,sum)
则对1到23的所有奇数进行求和,结果为:
[1] 144 156
同样,第二个参数改成2后,
apply(x,2,sum)
结果为:
[1] 84 100 116
是按照第二个维度进行的求和计算。
第二个参数改成3后,
apply(x,3,sum)
结果为:
[1] 21 57 93 129
即对第三个维度进行求和计算。
对于apply()函数的第三个参数,制定了具体用什么函数进行计算,之前例子里使用的都是sum,对于用户自定义的函数同样可以在这里使用。还用前面的矩阵b来举例。
定义了一个求平方和的函数:
myfun <- function(x){
sum(x^2)
}
如果选择按行计算平方和,可使用如下指令:
apply(b,1,myfun)
计算结果为:
one two three
172945
通常情况大家使用apply之后是需要把apply的返回值作为输入在其他代码中使用的,这里尤其重要的一点是apply的返回值的维度。上面的例子就算每一行或者每一列的sum,使用apply之后,返回都是一个向量,并不会因为apply计算行(列)的sum就会自动返回一个列(行)向量。
heatmap函数可以用来画heatmap图,例:require(graphics)require(grDevices)
x <- as.matrix(mtcars)
rc <- rainbow(nrow(x), start = 0, end = .3)
cc <- rainbow(ncol(x), start = 0, end = .3)
hv <- heatmap(x, col = cm.colors(256), scale = "column",
RowSideColors = rc, ColSideColors = cc, margins = c(5,10),
xlab = "specification variables", ylab = "Car Models",
main = "heatmap(<Mtcars data>, ..., scale = \"column\")")
utils::str(hv) # the two re-ordering index vectors