R语言之可视化①③散点图+拟合曲线
======================================
散点图一般用于展示两个变量之间的关系(比如线性相关)例如两个基因表达量的相关性。
cor.test(datagene2)
Pearson's product-moment correlation
data: data gene1 and data$gene2
t = 2.4858, df = 395, p-value = 0.0133495 percent confidence interval:
0.02600102 0.21984192cor 0.1241053
实例:通过以下代码计算两个基因的相关性
①使用ggplot2绘制
p1 <- ggplot(data = data, mapping = aes(x = gene1, y = gene2)) + geom_point(colour = "#426671", size = 2) + geom_smooth(method = lm,colour='#764C29',fill='#E7E1D7') p1 <- p1+ stat_cor(method = "pearson", label.x = 0.15, label.y = 30)+xlim(0,0.44) p1 p1 <- p1 + xlab("gene1") + theme(axis.title.x = element_text(size = 16, face = "bold", vjust = 0.5, hjust = 0.5))+ ylab("gene2") + theme(axis.title.y = element_text(size = 16, face = "bold", vjust = 0.5, hjust = 0.5))+ theme_bw() p1
②使用ggscatter绘制
ggscatter(data, x = "gene1", y = "gene2", color = "#426671", size =2, # Points color, shape and size add = "reg.line", # Add regressin line add.params = list(color = "#764C29", fill = "#E7E1D7"), # Customize reg. line conf.int = TRUE, # Add confidence interval cor.coef = TRUE, # Add correlation coefficient. see ?stat_cor cor.coeff.args = list(method = "pearson", label.x = 3, label.sep = "\n") )+stat_cor(method = "pearson", label.x = 0.15, label.y = 30)+xlim(0,0.44)+ xlab("gene1") + ylab('gene2) theme(axis.title.x = element_text(size = 16, face = "bold", vjust = 0.5, hjust = 0.5))+ ylab("gene2") + theme(axis.title.y = element_text(size = 16, face = "bold", vjust = 0.5, hjust = 0.5))+ theme_bw() p1
可以看出两个基因关联性并不高。
一些ggscatter的例子
set.seed(1234) dat <- data.frame(cond = rep(c("A", "B"), each=10), xvar = 1:20 + rnorm(20,sd=3), yvar = 1:20 + rnorm(20,sd=3))head(dat)library(ggplot2)
绘制最基本的线性回归图
ggplot(dat, aes(x=xvar, y=yvar)) + geom_point(shape=1) # Use hollow circlesggplot(dat, aes(x=xvar, y=yvar)) + geom_point(shape=1) + # Use hollow circles geom_smooth(method=lm) # Add linear regression line # (by default includes 95% confidence region)ggplot(dat, aes(x=xvar, y=yvar)) + geom_point(shape=1) + # Use hollow circles geom_smooth(method=lm, # Add linear regression line se=FALSE) # Don't add shaded confidence regionggplot(dat, aes(x=xvar, y=yvar)) + geom_point(shape=1) + # Use hollow circles geom_smooth() # Add a loess smoothed fit curve with confidence region#> `geom_smooth()` using method = 'loess'
可以自定义设置点的颜色和大小
# Set color by condggplot(dat, aes(x=xvar, y=yvar, color=cond)) + geom_point(shape=1)# Same, but with different colors and add regression linesggplot(dat, aes(x=xvar, y=yvar, color=cond)) + geom_point(shape=1) + scale_colour_hue(l=50) + # Use a slightly darker palette than normal geom_smooth(method=lm, # Add linear regression lines se=FALSE) # Don't add shaded confidence region# Extend the regression lines beyond the domain of the dataggplot(dat, aes(x=xvar, y=yvar, color=cond)) + geom_point(shape=1) + scale_colour_hue(l=50) + # Use a slightly darker palette than normal geom_smooth(method=lm, # Add linear regression lines se=FALSE, # Don't add shaded confidence region fullrange=TRUE) # Extend regression lines# Set shape by condggplot(dat, aes(x=xvar, y=yvar, shape=cond)) + geom_point()# Same, but with different shapesggplot(dat, aes(x=xvar, y=yvar, shape=cond)) + geom_point() + scale_shape_manual(values=c(1,2)) # Use a hollow circle and triangle
作者:赛乾
链接:https://www.jianshu.com/p/f4fd994b50e0
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦