如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)
1、首先数据均一化2、
#载入一个CSV文件,header=TRUE保证表头不被当成表格数据,sep=","用逗号把数据分开
mydata<-read.table("abc.csv",header=TRUE,sep=",")
library(ggplot2)
sample.groups<- c(rep(1, 124), rep(2, 54), rep(3, 199))
#把X那一列分成3组,rep(1,124)的意思是124个1,rep(2,54)的意思是54个2,这些都是按照X列的数字设定的,便于将A组的数据分成不同组。
qplot(x=PC1,y=PC2, data=mydata,colour=factor(sample.groups))+theme(legend.position="none")+stat_ellipse(lwd=1)
#x=PC1,y=PC2的意思是设定x,y轴为PC1和PC2那两列。colour=factor(sample.groups)设定3组不同颜色。theme(legend.position="none")是把图例去掉。stat_ellipse(lwd=1)是将不同组的点加上椭圆,lwd=1设定椭圆圈的粗度为1
3、高级选项
给图中的每个点添加标签
library(ggrepel)
#图上显示每个点的标签(标签的名称是A列)
label=mydata$A
#60-141行的标签为空格
label[60:141]=""
qplot(x=PC1,y=PC2, data=mydata,colour=factor(sample.groups))+theme(legend.position="none")+stat_ellipse(lwd=1)+geom_text_repel(label=label)
可以用内置的graphic包来画,就是plot()和curve()
也可以用ggplot2来画,后者更灵活。
graphic
# 先生成一组随机数x <- rnorm(2000)
# 画频率直方图, 分30个bin
hist(x, freq = F, breaks = 30)
# 再画概率分布曲线
lines(density(x, bw=.5), col="red", lwd=2)
2. ggplot2
# 准备工作, 把x设成一个数据集library(ggplot2)
data <- data.frame(x = x)
# 生成底层和直方图,概率线的图层
p <- ggplot(data, aes(x = x, y = ..density..))
p <- p + geom_histogram(fill = "navy")
p <- p + geom_density(colour = "green")
p
画出来风格不太一样,看你口味了