溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

R語言ggplot2畫圖比較兩組連續(xù)型數(shù)據(jù)的幾種方案分別是怎樣的

發(fā)布時(shí)間:2021-11-22 14:55:35 來源:億速云 閱讀:706 作者:柒染 欄目:大數(shù)據(jù)

R語言ggplot2畫圖比較兩組連續(xù)型數(shù)據(jù)的幾種方案分別是怎樣的,很多新手對此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

連續(xù)型數(shù)據(jù)的的分組比較在科研生活中非常常見,比如:實(shí)驗(yàn)組和對照組基因表達(dá)量的比較臨床病人存活組和死亡組某項(xiàng)檢查指標(biāo)的比較 等等。檢驗(yàn)兩組連續(xù)型數(shù)據(jù)之間是否存在差異通常會(huì)使用T檢驗(yàn)。對數(shù)據(jù)進(jìn)行展示通常可以使用柱形圖,箱線圖小提琴圖,直方圖,散點(diǎn)圖等幾種方式。今天的推文分別介紹一下以上5種圖形的ggplot2實(shí)現(xiàn)代碼。

 以下代碼用到3個(gè)R語言包

分別是ggplot2 用來畫圖RColorBrewer 用來生成顏色dplyr 用來整理數(shù)據(jù)

ggplot2dplyr如果是第一次使用需要安裝,安裝用到的命令是

install.packages("ggplot2")
install.packages("dplyr")
   首先是模擬數(shù)據(jù)集
set.seed(1234)
crp1<-round(abs(rnorm(200, mean = 150, sd = 48)))
status1<-rep("Death", 200)
data1<-data.frame(crp1, status1)
crp2<-round(abs(rnorm(200, mean = 70, sd = 20)))
status2<-rep("Survivor", 200)
data2<-data.frame(crp2, status2)
colnames(data1)[1]<-"CRP"
colnames(data1)[2]<-"Status"
colnames(data2)[1]<-"CRP"
colnames(data2)[2]<-"Status"
data<-rbind(data1, data2)
View(data)
 

得到的數(shù)據(jù)集data是包含兩個(gè)變量,分別是CRP和Status。模擬的是臨床病人存活者和死亡者C反應(yīng)蛋白(CRP)的差異。

R語言ggplot2畫圖比較兩組連續(xù)型數(shù)據(jù)的幾種方案分別是怎樣的  
image.png

接下來我們就來看看分別可以用哪些圖來展示這樣的數(shù)據(jù)

 帶誤差線的柱形圖

首先是對數(shù)據(jù)集進(jìn)行轉(zhuǎn)換

library(dplyr)
df1<-summarise(group_by(data,Status), mean(CRP))
df2<-summarise(group_by(data,Status), sd(CRP))
df3<-left_join(df1, df2)
View(df3)
 
R語言ggplot2畫圖比較兩組連續(xù)型數(shù)據(jù)的幾種方案分別是怎樣的  
image.png

畫圖

library(ggplot2)
ggplot(df3, aes(Status,`mean(CRP)`))+
  geom_col(aes(fill=Status),width=0.7,color="black")+
  geom_errorbar(aes(ymin = `mean(CRP)`, 
                    ymax = `mean(CRP)`+`sd(CRP)`), 
                width = 0.2)+
  scale_fill_brewer(palette = "Dark2")+
  theme(legend.position = "none")
 
R語言ggplot2畫圖比較兩組連續(xù)型數(shù)據(jù)的幾種方案分別是怎樣的  
image.png
 箱線圖
ggplot(data, aes(Status, CRP))+
  geom_boxplot(aes(fill=Status), width=0.6)+
  scale_fill_brewer(palette = "Dark2")+
  theme(legend.position = "none")
 

箱線圖比較常用,這里我給他們上了個(gè)色。分組信息在x軸已經(jīng)體現(xiàn),故去除圖例,避免累贅。出圖如下:R語言ggplot2畫圖比較兩組連續(xù)型數(shù)據(jù)的幾種方案分別是怎樣的

 小提琴圖
ggplot(data, aes(Status, CRP))+
  geom_violin(aes(fill = Status))+
  geom_boxplot(width=0.1)+
  scale_fill_brewer(palette = "Dark2")+
  theme(legend.position = "none")
 

這個(gè)圖實(shí)際上是小提琴圖和箱線圖的組合。小提琴圖的優(yōu)點(diǎn)在于能夠直觀地看到數(shù)據(jù)的分布情況。R語言ggplot2畫圖比較兩組連續(xù)型數(shù)據(jù)的幾種方案分別是怎樣的

 直方圖
ggplot(data)+
  geom_histogram(aes(CRP, fill=Status), position = "identity", alpha=0.6, color="white")+
  scale_fill_brewer(palette = "Dark2")
 

直方圖同樣也能看出數(shù)據(jù)的分布。但這里因?yàn)閳D形有重疊,我們需要用alpha參數(shù)對透明度進(jìn)行設(shè)置。

R語言ggplot2畫圖比較兩組連續(xù)型數(shù)據(jù)的幾種方案分別是怎樣的此外,在直方圖的基礎(chǔ)上,我們也可以添加核密度曲線:

ggplot(data, aes(CRP))+
  geom_histogram(aes(y = ..density.., fill = Status), position = "identity", alpha = 0.6, color = "white")+
  stat_density(geom = 'line', size=1, position = 'identity', aes(color = Status))+
  scale_fill_brewer(palette = "Dark2")+
  scale_color_brewer(palette = "Dark2")
 
R語言ggplot2畫圖比較兩組連續(xù)型數(shù)據(jù)的幾種方案分別是怎樣的  
image.png
 散點(diǎn)圖
ggplot(data, aes(Status, CRP))+
  geom_jitter(shape=21, size=4, color="black", aes( fill = Status), width=0.2)+
  scale_fill_brewer(palette = "Dark2") +
  theme(legend.position = "none")
 
R語言ggplot2畫圖比較兩組連續(xù)型數(shù)據(jù)的幾種方案分別是怎樣的  
image.png

散點(diǎn)圖用到的主要圖形對象包括geom_jitter和geom_dotplot. geom_jitter產(chǎn)生的點(diǎn)可在一定范圍內(nèi)隨機(jī)波動(dòng),所以也叫抖動(dòng)點(diǎn)圖;而geom_dotplot產(chǎn)生的點(diǎn)可以按照作者想要的方式(比如,居中)進(jìn)行排列。

ggplot(data, aes(Status, CRP)) +
  geom_dotplot(binaxis = "y", stackdir = "center",aes(fill = Status), binwidth = 6) +
  scale_fill_brewer(palette = "Dark2") +
  theme(legend.position = "none")
 
R語言ggplot2畫圖比較兩組連續(xù)型數(shù)據(jù)的幾種方案分別是怎樣的    

除了以上幾種圖形之外,還可以對多種圖形對象進(jìn)行組合,比如點(diǎn)圖+柱狀圖,點(diǎn)圖+箱線圖。這樣可使數(shù)據(jù)的展現(xiàn)更為飽滿。

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI