前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >R语言之可视化①③散点图+拟合曲线目录

R语言之可视化①③散点图+拟合曲线目录

作者头像
用户1359560
发布2018-12-14 10:57:52
2.2K0
发布2018-12-14 10:57:52
举报

散点图一般用于展示两个变量之间的关系(比如线性相关)例如两个基因表达量的相关性。

cor.test(data

gene1,data
gene1,data

gene2) Pearson's product-moment correlation data: data gene1 and data$gene2 t = 2.4858, df = 395, p-value = 0.01334 95 percent confidence interval: 0.02600102 0.21984192 cor 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 circles

ggplot(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 region

ggplot(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 cond
ggplot(dat, aes(x=xvar, y=yvar, color=cond)) + geom_point(shape=1)

# Same, but with different colors and add regression lines
ggplot(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 data
ggplot(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 cond
ggplot(dat, aes(x=xvar, y=yvar, shape=cond)) + geom_point()

# Same, but with different shapes
ggplot(dat, aes(x=xvar, y=yvar, shape=cond)) + geom_point() +
    scale_shape_manual(values=c(1,2))  # Use a hollow circle and triangle
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.11.18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一些ggscatter的例子
    • 绘制最基本的线性回归图
      • 可以自定义设置点的颜色和大小
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档