R語言繪制帶ErrorBar的分組條形圖代碼的分享_第1頁
R語言繪制帶ErrorBar的分組條形圖代碼的分享_第2頁
R語言繪制帶ErrorBar的分組條形圖代碼的分享_第3頁
R語言繪制帶ErrorBar的分組條形圖代碼的分享_第4頁
R語言繪制帶ErrorBar的分組條形圖代碼的分享_第5頁
已閱讀5頁,還剩2頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

第R語言繪制帶ErrorBar的分組條形圖代碼的分享目錄第一種實(shí)現(xiàn)方法:用aggregate計(jì)算數(shù)據(jù)第二種實(shí)現(xiàn)方法:用dplyr包計(jì)算數(shù)據(jù)筆者近期畫了一張帶errorbar的分組條形圖,將相關(guān)的代碼分享一下。

感謝網(wǎng)友青山屋主的建議,提示筆者要嚴(yán)謹(jǐn)區(qū)分技術(shù)重復(fù)和生物學(xué)重復(fù),所以筆者對(duì)文章做修改后重發(fā)。如果各位有任何建議,歡迎指正。

本文旨在給出一種利用R對(duì)生物學(xué)重復(fù)數(shù)據(jù)畫帶errorbar的分組條形圖的方法。

所用數(shù)據(jù)是模擬生成的:分成三個(gè)組,每個(gè)組進(jìn)行了若干次生物學(xué)重復(fù);測(cè)量的是3種基因的表達(dá)量。數(shù)據(jù)的部分內(nèi)容如下:

##gene1gene2gene3Group

##149.72475267.0007126.2007Group1

##2114.62184173.8780150.2641Group2

##3128.03351227.9456152.6378Group3

##4134.90841385.1979148.2739Group1

##5136.56659190.0663122.6201Group2

##6143.88241329.0516236.9131Group3

兩種方法的完整代碼放在了文末。如有問題,歡迎指正!

第一種實(shí)現(xiàn)方法:用aggregate計(jì)算數(shù)據(jù)

#導(dǎo)入數(shù)據(jù)

setwd("E:/")

df-read.csv("gene_exp.csv",header=T)

#可以在這里改列名,這些列名就是最終圖上X軸的標(biāo)簽名。

colnames(df)[1:3]-c("gene-1","gene-2","gene-3")

str(df)#顯示數(shù)據(jù)集內(nèi)容

##'data.frame':3000obs.of4variables:

##$gene-1:num49.7114.6128134.9136.6...

##$gene-2:num267174228385190...

##$gene-3:num126150153148123...

##$Group:Factorw/3levels"Group1","Group2",..:1231231231...

#將上述"寬數(shù)據(jù)"轉(zhuǎn)化為"長數(shù)據(jù)"

library(reshape2)

df_reshape-melt(df,id.vars=c("Group"))

str(df_reshape)

##'data.frame':9000obs.of3variables:

##$Group:Factorw/3levels"Group1","Group2",..:1231231231...

##$variable:Factorw/3levels"gene-1","gene-2",..:1111111111...

##$value:num49.7114.6128134.9136.6...

#獲取三個(gè)組各個(gè)基因表達(dá)量的平均值

df_mean-aggregate(df_reshape$value,list(Group=df_reshape$Group,

gene=df_reshape$variable),mean,na.rm=T)

#獲取三個(gè)組各個(gè)基因表達(dá)量的標(biāo)準(zhǔn)差

df_sd-aggregate(df_reshape$value,list(Group=df_reshape$Group,

gene=df_reshape$variable),sd,na.rm=T)

#合并mean和sd

colnames(df_mean)[3]-"mean"

colnames(df_sd)[3]-"sd"

df_stat-merge(df_mean,df_sd,by=c("Group","gene"))

str(df_stat)

##'data.frame':9obs.of4variables:

##$Group:Factorw/3levels"Group1","Group2",..:111222333

##$gene:Factorw/3levels"gene-1","gene-2",..:123123123

##$mean:num120249149119250...

##$sd:num19.451.430.221.252.3...

#畫圖

#直接在畫圖的語句中計(jì)算出error_bar所需的數(shù)據(jù):

#(即下面的ymin=mean-sd和ymax=mean+sd語句)。

library(ggplot2)

dodge-position_dodge(width=.9)

ggplot(data=df_stat)+

geom_bar(aes(x=gene,y=mean,fill=Group),

stat="identity",position=dodge)+

geom_errorbar(aes(x=gene,ymin=mean-sd,ymax=mean+sd,color=Group),

stat="identity",position=dodge,width=.3)

第二種實(shí)現(xiàn)方法:用dplyr包計(jì)算數(shù)據(jù)

#導(dǎo)入數(shù)據(jù)

setwd("E:/")

df-read.csv("gene_exp.csv",header=T)

#可以在這里改列名,這些列名就是最終圖上X軸的標(biāo)簽名。

colnames(df)[1:3]-c("gene-1","gene-2","gene-3")

str(df)#顯示數(shù)據(jù)集內(nèi)容

##'data.frame':3000obs.of4variables:

##$gene-1:num49.7114.6128134.9136.6...

##$gene-2:num267174228385190...

##$gene-3:num126150153148123...

##$Group:Factorw/3levels"Group1","Group2",..:1231231231...

#獲取三個(gè)組各個(gè)基因表達(dá)量的平均值和標(biāo)準(zhǔn)差

library(tidyr)

library(dplyr)

df_stat-tbl_df(df)%%

gather(gene,value,-Group)%%#將"寬數(shù)據(jù)"轉(zhuǎn)化為"長數(shù)據(jù)"

group_by(Group,gene)%%#將數(shù)據(jù)分組

summarise(mean=mean(value,na.rm=T),sd=sd(value,na.rm=T))%%#計(jì)算每組數(shù)據(jù)的mean和sd

ungroup()

str(df_stat)

##Classes'tbl_df','tbl'and'data.frame':9obs.of4variables:

##$Group:Factorw/3levels"Group1","Group2",..:111222333

##$gene:chr"gene-1""gene-2""gene-3""gene-1"...

##$mean:num120249149119250...

##$sd:num19.451.430.221.252.3...

#畫圖

#直接在畫圖的語句中計(jì)算出error_bar所需的數(shù)據(jù):

#(即下面的ymin=mean-sd和ymax=mean+sd語句)。

library(ggplot2)

dodge-position_dodge(width=.9)

df_stat%%ggplot()+

geom_bar(aes(x=gene,y=mean,fill=Group),

stat="identity",position=dodge)+

geom_errorbar(aes(x=gene,ymin=mean-sd,ymax=mean+sd,color=Group),

stat="identity",position=dodge,width=.3)

兩種方法的結(jié)果是一樣的,相對(duì)而言,dplyr的實(shí)現(xiàn)方法更簡單快捷。

最后,兩種方法的完整代碼如下:

#################第一種實(shí)現(xiàn)方法:用aggregate計(jì)算數(shù)據(jù)######################

#導(dǎo)入數(shù)據(jù)

setwd("E:/")

df-read.csv("gene_exp.csv",header=T)

#可以在這里改列名,這些列名就是最終圖上X軸的標(biāo)簽名。

colnames(df)[1:3]-c("gene-1","gene-2","gene-3")

str(df)#顯示數(shù)據(jù)集內(nèi)容

#將上述"寬數(shù)據(jù)"轉(zhuǎn)化為"長數(shù)據(jù)"

library(reshape2)

df_reshape-melt(df,id.vars=c("Group"))

str(df_reshape)

#獲取三個(gè)組各個(gè)基因表達(dá)量的平均值

df_mean-aggregate(df_reshape$value,list(Group=df_reshape$Group,

gene=df_reshape$variable),mean,na.rm=T)

#獲取三個(gè)組各個(gè)基因表達(dá)量的標(biāo)準(zhǔn)差

df_sd-aggregate(df_reshape$value,list(Group=df_reshape$Group,

gene=df_reshape$variable),sd,na.rm=T)

#合并mean和sd

colnames(df_mean)[3]-"mean"

colnames(df_sd)[3]-"sd"

df_stat-merge(df_mean,df_sd,by=c("Group","gene"))

str(df_stat)

#直接在畫圖的語句中計(jì)算出error_bar所需的數(shù)據(jù):

#(即下面的ymin=mean-sd和ymax=mean+sd語句)。

library(ggplot2)

dodge-position_dodge(width=.9)

ggplot(data=df_stat)+

geom_bar(aes(x=gene,y=mean,fill=Group),

stat="identity",position=dodge)+

geom_errorbar(aes(x=gene,ymin=mean-sd,ymax=mean+sd,color=Group),

stat="identity",position=dodge,width=.3)

####################第二種實(shí)現(xiàn)方法:用dplyr包計(jì)算數(shù)據(jù)######################

#導(dǎo)入數(shù)據(jù)

setwd("E:/")

df-read.csv("gene_exp.csv",header=T)

#可以在這里改列名,這些列名就是最終圖上X軸的標(biāo)簽名。

colnames(df)[1:3]-c("gene-1","gene-2","gene-3")

str(df)#顯示數(shù)據(jù)集內(nèi)容

#獲取三個(gè)組各個(gè)基因表達(dá)量的平均值和標(biāo)準(zhǔn)差

library(tidyr)

library(dplyr)

df_stat-tbl_df(df)%%

gather(gene,value,-Group)%%#將"寬數(shù)據(jù)"轉(zhuǎn)化為"長數(shù)據(jù)"

group_by(Group,gene)%%#將數(shù)據(jù)分組

summarise(mean=mean(value,na.rm=T),sd=sd(value,na.rm=T))%%#計(jì)算每組數(shù)據(jù)的mean和sd

ungroup()

str(df_stat)

#直接在畫圖的語句中計(jì)算出error_bar所需的數(shù)據(jù):

#(即下面的ymin=mean-sd和ymax=mean+sd語句)。

library(ggplot2)

dodge-position_dodge(width=.9)

df_stat%%ggp

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論