【R】表達矩陣指定繪制兩樣本的相關性散點圖?


表達矩陣

image.png

要做兩兩樣本的相關性散點圖,並計算標明相關系數。

編寫函數要點:

  • 直接在aes中傳參是不行的
  • 線性回歸表達式

函數

方法1:用!!ensym

myplot <- function(indata, inx, iny){
  nms <- names(indata)
  x <- nms[inx]
  y <- nms[iny]
  regression <- paste0(x, " ~ ", y)
  dat.lm <- lm(as.formula(regression), data = indata)
  r <- sprintf("italic(r) == %.2f",sqrt(summary(dat.lm)$r.squared))
  labels <- data.frame(r=r,stringsAsFactors = FALSE)
  
  ggplot(indata,aes(x=!!ensym(x), y=!!ensym(y)))+geom_point() + 
    geom_smooth(method = lm) + 
    labs(x=paste0(x," (log2 intensity)"),y=paste0(y," (log2 intensity)")) +
    geom_text(data=labels,mapping=aes(x = 15,y=30,label=r),parse = TRUE,inherit.aes = FALSE,size = 6)
}

p1 <- myplot(indata=dia,inx=2,iny=3)

image.png

方法2:用environment

showplot1<-function(indata, inx, iny) {
  dat <- indata
  p <- ggplot(dat, aes(x=dat[,inx], y=dat[,iny]), environment = environment())
  p <- p + geom_point()
  print(p)
}
showplot1(dia,2,3)

image.png

方法3:用aes_string

showplot1 <- function(indata, inx, iny) {
  x <- names(indata)[inx] 
  y <- names(indata)[iny] 
  p <- ggplot(indata, aes_string(x = x, y = y))
  p + geom_point()
}
showplot1(dia,2,3)

image.png

兩兩樣本的相關性散點圖可以用循環生成組合圖。不贅述。

Ref: https://stackoverflow.com/questions/15323269/addressing-x-and-y-in-aes-by-variable-number


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM