小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

文獻(xiàn)配套GitHub發(fā)表級(jí)別繪圖之餅圖

 健明 2022-01-26

給學(xué)徒們收集整理了幾套帶GitHub源代碼的文獻(xiàn)圖表合輯,讓優(yōu)秀者一點(diǎn)一滴拆解開(kāi)來(lái)分享給大家。(全部的代碼復(fù)制粘貼即可運(yùn)行,歡迎嘗試以及批評(píng)指正)

下面是去年實(shí)習(xí)生的分享

author: "ylchen" 

ggplot2是R語(yǔ)言最流行的第三方擴(kuò)展包,是RStudio首席科學(xué)家Hadley Wickham讀博期間的作品。根據(jù)其繪圖理念,圖形由以下幾個(gè)模塊組成:

簡(jiǎn)單來(lái)說(shuō):

一張統(tǒng)計(jì)圖形就是從數(shù)據(jù)到幾何對(duì)象(geometric object,縮寫geom)的圖形屬性(aesthetic attribute,縮寫aes)的一個(gè)映射。此外,圖形中還可能包含數(shù)據(jù)的統(tǒng)計(jì)變換(statistical transformation,縮寫stats),最后繪制在某個(gè)特定的坐標(biāo)系(coordinate system,縮寫coord)中,而分面(facet)則可以用來(lái)生成數(shù)據(jù)不同子集的圖形。

接下來(lái)開(kāi)啟ggplot2基礎(chǔ)學(xué)習(xí)系列。第一步從復(fù)現(xiàn)文章開(kāi)始。

文章來(lái)源:"Preoperative immune landscape predisposes adverse outcomes in hepatocellular carcinoma patients with liver transplantation" (2021,npj Precision Oncology),數(shù)據(jù)與代碼全部公開(kāi)在https://github.com/sangho1130/KOR_HCC。

下面首先來(lái)復(fù)現(xiàn)Fig.1a(pie chart& rose chart),用餅圖呈現(xiàn)出不同組織的數(shù)量

Fig.1

一、數(shù)據(jù)載入

  library(ggplot2)
library(scales)
library(RColorBrewer)
#構(gòu)建數(shù)據(jù)框結(jié)構(gòu)
data <- data.frame(
group = c('Normal', 'FL', 'FH', 'CS', 'DL', 'DH', 'T1', 'T2', 'T3', 'Mixed'),
value = c(16, 10, 10, 10, 10, 7, 17, 30, 11, 7)
)
Colors <- c('#bebdbd', '#bbe165', '#6e8a3c', '#546a2e',
'#f1c055', '#eb8919', '#f69693', '#f7474e', '#aa0c0b', '#570a08')
#分類數(shù)據(jù)轉(zhuǎn)換成因子變量,需要調(diào)整順序的話可以加上level參數(shù)
data$group <- factor(data$group)
head(data)

二、首先生成柱狀圖

開(kāi)始選擇合適的幾何對(duì)象,就是用什么樣的圖片來(lái)直觀表示我們的數(shù)據(jù)。其中柱狀圖就用geom_bar函數(shù)。除此之外,ggplot2提供了眾多幾何對(duì)象geom_xyz()供大家選擇,完整的幾何對(duì)象可以下載RStudio公司總結(jié)的ggplot2 cheetsheet。

ggplot(data,                         #輸入數(shù)據(jù)
aes(x="", #設(shè)置x軸變量
y=value, #設(shè)置y軸變量
fill=factor(group))) + #設(shè)置圖形填充變量
geom_bar(width = 1, #geom_bar()函數(shù)繪制條形圖,width:條形圖的寬度
stat = "identity")+ #stat="count"表示條形的高度是y變量的數(shù)量
#stat="identity"表示條形的高度是y變量的值
scale_fill_manual(values = Colors) #設(shè)置填充的顏色

三、轉(zhuǎn)換成餅圖

  • coord_polar將圓形轉(zhuǎn)成極坐標(biāo)
ggplot(data,                         #輸入數(shù)據(jù)
aes(x="", #設(shè)置x軸變量
y=value, #設(shè)置y軸變量
fill=factor(group))) + #設(shè)置圖形填充變量
geom_bar(width = 1, #geom_bar()函數(shù)繪制條形圖,width:條形圖的寬度
color = "white",
stat = "identity")+ #stat="count"表示條形的高度是y變量的數(shù)量
#stat="identity"表示條形的高度是y變量的值
scale_fill_manual(values = Colors) + #設(shè)置填充的顏色 #coord_polar極坐標(biāo)轉(zhuǎn)換,可以做餅圖或玫瑰圖或雷達(dá)圖
coord_polar(theta = "y", #theta 可以選擇x或y,表示外延的坐標(biāo)
direction = -1) + #direction表示數(shù)據(jù)方向,1是順時(shí)針,-1是逆時(shí)針
theme_void() + #theme_void(): 設(shè)置完全空白的主題
#ggtitle("I'm a titile")+ #添加標(biāo)題
theme(plot.title = element_text(hjust = 1), #hjust調(diào)整水平距離,
#element_text()設(shè)置基于文本的組件
legend.title = element_blank())+ #不顯示legend標(biāo)題
geom_text(aes(label = value),
position = position_stack(vjust = 0.5),size=5) #vjust=0.5在堆疊柱子的中間位置添加文字

四、玫瑰圖

  • 然而,當(dāng)我們的數(shù)據(jù)不是代表個(gè)體占總體的百分比,而是強(qiáng)調(diào)數(shù)據(jù)大小的對(duì)比時(shí),可以用玫瑰圖來(lái)展示,根據(jù)上面的代碼做簡(jiǎn)單的修改即可。
  • 其實(shí)就是修改參數(shù):coord_polar(theta = 'x',start = 0,direction = 1)
ggplot(data,                           
aes(x=group,y=value,
fill=factor(group))) +
geom_bar(
width = 1,
stat = "identity") +
scale_fill_manual(values = Colors) +
coord_polar(theta = 'x',start = 0,direction = 1)+
theme_void() +
theme(plot.title = element_text(hjust = 0.5),
legend.title = element_blank())

五、劃重點(diǎn)了!

function函數(shù)

學(xué)習(xí)代碼的目的就是為了節(jié)省我們的繪圖時(shí)間。我們可以構(gòu)建function函數(shù),下次輸入自己數(shù)據(jù)就可以直接使用,不用再調(diào)整參數(shù)。

復(fù)習(xí)function函數(shù)的書寫,function函數(shù)定義如下

function_name <- function(arg1,arg2,....){
functionBody
}
#function_name函數(shù)名
#arg1,arg2,....函數(shù)的參數(shù)
#functionBody函數(shù)主體,定義函數(shù)的作用
# 構(gòu)建一個(gè)繪圖函數(shù)
drPiechart <- function(columnNames, Values, Colors){
library(ggplot2)
library(scales)
library(RColorBrewer)
#構(gòu)建數(shù)據(jù)框結(jié)構(gòu)
data <- data.frame(
group = columnNames,
value = Values
)
data$group <- factor(data$group, columnNames) #轉(zhuǎn)換成因子變量
#繪圖
ggplot(data, #輸入數(shù)據(jù)
aes(x="", #設(shè)置x軸變量
y=value, #設(shè)置y軸變量
fill=factor(group))) + #設(shè)置圖形填充變量
geom_bar(width = 1, #geom_bar()函數(shù)繪制條形圖,width:條形圖的寬度
stat = "identity")+ #stat="count"表示條形的高度是y變量的數(shù)量
#stat="identity"表示條形的高度是y變量的值
scale_fill_manual(values = Colors) + #設(shè)置填充的顏色 #coord_polar極坐標(biāo)轉(zhuǎn)換,可以做餅圖或玫瑰圖或雷達(dá)圖
coord_polar(theta = "y", #theta 可以選擇x或y,表示外延的坐標(biāo)
direction = -1) + #direction表示數(shù)據(jù)方向,1是順時(shí)針,-1是逆時(shí)針
theme_void() + #theme_void(): 設(shè)置完全空白的主題
theme(plot.title = element_text(hjust = 1), #hjust調(diào)整水平距離,
#element_text()設(shè)置基于文本的組件
legend.title = element_blank())+ #不顯示legend標(biāo)題
geom_text(aes(label = value),
position = position_stack(vjust = 0.5),size=5) #vjust=0.5在堆疊柱子的中間位置添加文字
}

輸入自己數(shù)據(jù)即可作圖

#依次按照columnNames, Values, Colors填入自己的數(shù)據(jù)即可
drPiechart(c('Normal', 'FL', 'FH', 'CS', 'DL', 'DH', 'T1', 'T2', 'T3', 'Mixed'),
c(16, 10, 10, 10, 10, 7, 17, 30, 11, 7),
c('#bebdbd', '#bbe165', '#6e8a3c', '#546a2e',
'#f1c055', '#eb8919', '#f69693', '#f7474e', '#aa0c0b', '#570a08'))

六、進(jìn)階的餅圖

  • 如果需要展示兩種分類形式,可以將餅圖和環(huán)形圖結(jié)合起來(lái)
  • 參考:https:///cardiomoon/398623
# if(!require(devtools)) install.packages("devtools")
# devtools::install_github("cardiomoon/moonBook")
# devtools::install_github("cardiomoon/webr")
library(ggplot2)
library(moonBook)
library(webr)

package moonBook 里面包含857名急性冠脈綜合征(acs)患者的人口統(tǒng)計(jì)和實(shí)驗(yàn)室數(shù)據(jù),這里展示吸煙狀態(tài)的分布

head(acs)
# labelposition=0.1 百分?jǐn)?shù)小于10%,標(biāo)簽位于外部
# ratioByGroup=FALSE 按不同分組計(jì)算百分比
PieDonut(acs,aes(pies=Dx,donuts=smoking),
ratioByGroup=FALSE,labelposition=0.1,
title="Distribution of Smoking Status by Diagnosis")

標(biāo)簽位置

如果希望將標(biāo)簽全放在內(nèi)部,需將labelposition參數(shù)設(shè)置為0。在外面設(shè)置labelposition參數(shù)為1

PieDonut(acs,aes(Dx,smoking),selected=1,labelposition=0,title="labelposition=0")
PieDonut(acs,aes(Dx,smoking),selected=1,labelposition=1,title="labelposition=1")

突出展示

如果你想著重突出某個(gè)部分,這個(gè)參數(shù)就很方便了

參數(shù):explode 內(nèi)環(huán)的突出;selected 外環(huán)的突出

PieDonut(acs,aes(Dx,smoking),explode=1)
PieDonut(acs,aes(Dx,smoking),explode=1,explodeDonut=TRUE)
# explode 內(nèi)環(huán)的突出
# selected 外環(huán)的突出
PieDonut(acs,aes(Dx,smoking),explode=1,selected=c(3,6,9),explodeDonut=TRUE)

角度調(diào)整

PieDonut(acs,aes(Dx,smoking),start=3*pi/2,explode=1,selected=c(3,6,9),explodeDonut=TRUE)

半徑設(shè)置

如果你不想要中間的圓心,可通過(guò)r0showPieName設(shè)置

另外,r1,r2分別可以調(diào)整內(nèi)外環(huán)的半徑

PieDonut(acs,aes(Dx,smoking),r0=0,showPieName=FALSE)
# r1,r2分別可以調(diào)整內(nèi)外環(huán)的半徑
PieDonut(acs,aes(Dx,smoking),r0=0.2,r1=0.8,r2=1.4,explode=1,start=pi/2,explodeDonut=TRUE)

    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多