?;鶊D的繪制(基礎(chǔ)知識(shí),R與origin)

最近年中總結(jié),我們學(xué)習(xí)一下基礎(chǔ)知識(shí)

R版本

?;鶊D繪制


清除當(dāng)前環(huán)境中的變量

rm(list=ls())

設(shè)置工作目錄

setwd("C:/Users/Dell/Desktop/R_Plots/23sankey/")

使用riverplot包繪制桑基圖

# 安裝并加載所需的R包
#install.packages("riverplot")
library(riverplot)

# 構(gòu)建測(cè)序數(shù)據(jù)集
nodes <- c( LETTERS[1:5] )
nodes
## [1] "A" "B" "C" "D" "E"

edges <- list( A = list( C= 6 ), 
               B = list( C= 5 ),
               C = list( D= 4 ),
               E = list( C= 3 )
               )
edges
## $A
## $A$C
## [1] 6
##
##
## $B
## $B$C
## [1] 5
##
##
## $C
## $C$D
## [1] 4
##
##
## $E
## $E$C
## [1] 3

# 使用makeRiver函數(shù)構(gòu)造riverplot對(duì)象
r <- makeRiver( nodes, edges, 
                node_xpos= c( 1,1,2,3,3 ),
                node_labels= c( A= "Node A", B= "Node B", C= "Node C", D= "Node D", E= "Node E" ),
                node_styles= list( A= list( col= "yellow" ), D= list( col= "blue" ), E= list( col= "red" )))
r
## $edges
##        ID N1 N2 Value
## A->C A->C  A  C     6
## B->C B->C  B  C     5
## C->D C->D  C  D     4
## E->C E->C  E  C     3
## 
## $nodes
##   ID x labels
## A  A 1 Node A
## B  B 1 Node B
## C  C 2 Node C
## D  D 3 Node D
## E  E 3 Node E
## 
## $styles
## $styles$A
## $styles$A$col
## [1] "yellow"
## 
## 
## $styles$D
## $styles$D$col
## [1] "blue"
## 
## 
## $styles$E
## $styles$E$col
## [1] "red"
## 
## 
## 
## attr(,"class")
## [1] "list"      "riverplot"

# 使用riverplot函數(shù)繪制?;鶊D
riverplot(r)

image
# 繪制一個(gè)DNA雙螺旋
# a DNA strand
plot.new()
par( usr= c( 0, 4, -2.5, 2.5 ) )

w <- 0.4
cols <- c( "blue", "green" )
init <- c( -0.8, -0.5 )
pos  <- c( 1, -1 )
step <- 0.5

# Draw a curved segment
for( i in rep( rep( c( 1, 2 ), each= 2 ), 5 ) ) {
  curveseg( init[i], init[i] + step, pos[1], pos[2], width= w, col= cols[i] )
  init[i] <- init[i] + step
  pos <- pos * -1
}

image

使用ggforce包繪制桑基圖

# 安裝并加載所需的R包
#install.packages("ggforce")
library(ggforce)

# 構(gòu)建示例數(shù)據(jù)
data <- reshape2::melt(Titanic)
head(data)
##  Class    Sex   Age Survived value
## 1   1st   Male Child       No     0
## 2   2nd   Male Child       No     0
## 3   3rd   Male Child       No    35
## 4  Crew   Male Child       No     0
## 5   1st Female Child       No     0
## 6   2nd Female Child       No     0

data <- gather_set_data(data, 1:4)
head(data)
##   Class    Sex   Age Survived value id     x    y
## 1   1st   Male Child       No     0  1 Class  1st
## 2   2nd   Male Child       No     0  2 Class  2nd
## 3   3rd   Male Child       No    35  3 Class  3rd
## 4  Crew   Male Child       No     0  4 Class Crew
## 5   1st Female Child       No     0  5 Class  1st
## 6   2nd Female Child       No     0  6 Class  2nd

# 使用geom_parallel_setsh函數(shù)繪制?;鶊D
ggplot(data, aes(x, id = id, split = y, value = value)) +
  geom_parallel_sets(aes(fill = Sex), alpha = 0.5, axis.width = 0.1) +
  geom_parallel_sets_axes(axis.width = 0.2,fill="black",color="red") +
  geom_parallel_sets_labels(colour = 'white',angle = 45) +
  theme_bw()

image

使用ggalluvial包繪制?;鶊D

# 安裝并加載所需的R包
#install.packages("ggalluvial")
library(ggalluvial)

# 使用geom_alluvium函數(shù)繪制桑基圖
admissions <- as.data.frame(UCBAdmissions)
head(admissions)
##      Admit Gender Dept Freq
## 1 Admitted   Male    A  512
## 2 Rejected   Male    A  313
## 3 Admitted Female    A   89
## 4 Rejected Female    A   19
## 5 Admitted   Male    B  353
## 6 Rejected   Male    B  207

ggplot(admissions,
       aes(y = Freq, axis1 = Gender, axis2 = Dept)) +
  geom_alluvium(aes(fill = Admit), width = 1/12) +
  geom_stratum(width = 1/12, fill = "black", color = "grey") +
  geom_label(stat = "stratum", aes(label = after_stat(stratum))) +
  scale_x_discrete(limits = c("Gender", "Dept"), expand = c(.05, .05)) +
  scale_fill_brewer(type = "qual", palette = "Set1") +
  ggtitle("UC Berkeley admissions and rejections, by sex and department")

image
data <- as.data.frame(Titanic)
head(data)
##   Class    Sex   Age Survived Freq
## 1   1st   Male Child       No    0
## 2   2nd   Male Child       No    0
## 3   3rd   Male Child       No   35
## 4  Crew   Male Child       No    0
## 5   1st Female Child       No    0
## 6   2nd Female Child       No    0

ggplot(data,
       aes(y = Freq,
           axis1 = Survived, axis2 = Sex, axis3 = Class)) +
  geom_alluvium(aes(fill = Class),width = 0, 
                knot.pos = 0, reverse = FALSE) +
  guides(fill = FALSE) +
  geom_stratum(width = 1/8, reverse = FALSE) +
  geom_text(stat = "stratum", aes(label = after_stat(stratum)),reverse = FALSE) +
  scale_x_continuous(breaks = 1:3, labels = c("Survived", "Sex", "Class")) +
  coord_flip() +
  ggtitle("Titanic survival by class and sex")

image
data(vaccinations)
levels(vaccinations$response) <- rev(levels(vaccinations$response))
head(vaccinations)
##      survey freq subject response start_date   end_date
## 1 ms153_NSA   48       1  Missing 2010-09-22 2010-10-25
## 2 ms153_NSA    9       2  Missing 2010-09-22 2010-10-25
## 3 ms153_NSA   66       3  Missing 2010-09-22 2010-10-25
## 4 ms153_NSA    1       4  Missing 2010-09-22 2010-10-25
## 5 ms153_NSA   11       5  Missing 2010-09-22 2010-10-25
## 6 ms153_NSA    1       6  Missing 2010-09-22 2010-10-25

ggplot(vaccinations,
       aes(x = survey, stratum = response, alluvium = subject,
           y = freq,
           fill = response, label = response)) +
  scale_x_discrete(expand = c(.1, .1)) +
  geom_flow() +
  geom_stratum(alpha = .5) +
  geom_text(stat = "stratum", size = 4) +
  theme(legend.position = "none") +
  ggtitle("vaccination survey responses at three points in time")

image

origin版本(非代碼,手動(dòng)操作)

數(shù)據(jù)準(zhǔn)備

在之前的文章中有提到按“F11”鍵就可以快速進(jìn)入origin的“Origin central”直接套用模板去學(xué)習(xí)作圖,如下圖,在新版的Origin 2019已將“Origin centra1”改為“Learning center”。 今天我們就直接用它的實(shí)例數(shù)據(jù)作圖。

###

雙擊實(shí)例中的圖表即可打開(kāi)實(shí)例圖表的工程文件,我這里只需數(shù)據(jù),所以復(fù)制了一份到文件夾Folder1中,數(shù)據(jù)為歷史上著名的泰坦尼克號(hào)事故中旅客和船員的是否獲救記錄(如下圖)。數(shù)據(jù)只要是原始的分類記錄即可,不需要手動(dòng)計(jì)數(shù)。注意,Origin 2019的數(shù)據(jù)介紹弄錯(cuò)了,嗯,希望它接下來(lái)改進(jìn)吧。

image

雙坐標(biāo)軸圖

為了便于理解,我這里先畫(huà)兩個(gè)坐標(biāo)軸的桑基圖,看一下成人和小孩的幸存情況。類似Excel的操作,將鼠標(biāo)指針懸停在表格列名上然后選中3、4列數(shù)據(jù)(包括分組數(shù)據(jù)),如下。

image

然后進(jìn)入Plot菜單,找到Parallel Sets。

image

點(diǎn)擊一下Parallel Sets即可完成繪制,初始結(jié)果如下:

image

可見(jiàn)孩子的存活比例比成人高,雖然女人和孩子可優(yōu)先乘坐救生船,但仍有約一半的小孩沒(méi)能獲救。

三坐標(biāo)軸

接下來(lái)增加點(diǎn)復(fù)雜度,同樣的方法選擇數(shù)據(jù),這次加入性別數(shù)據(jù),如下圖。

image

繪制的初始結(jié)果如下:

image

類似于常規(guī)圖表,雙擊坐標(biāo)軸可以對(duì)坐標(biāo)軸的粗細(xì)、顏色、刻度朝向等進(jìn)行調(diào)整,如下。

image

雙擊圖表區(qū)域,在Plot Details窗口也可改變顏色,可選擇已有的配色方案,比如這里選Q05。

image

類似《如何用Origin繪制分邊小提琴圖》一文,如果對(duì)Q05的顏色不滿意,還可點(diǎn)右側(cè)的“?”,自定義顏色列表,如下圖。

image

調(diào)整配色后的結(jié)果如下:

image

勾選Combined Sets可將相同“流向”的數(shù)據(jù)進(jìn)行合并,如下圖。

image

具體的合并方式和結(jié)果見(jiàn)下圖:

image

四坐標(biāo)軸

同樣的方法將4列數(shù)據(jù)全選,繪制結(jié)果如下圖:

image

Transparency調(diào)整顏色的透明度,Curvature(曲率) 調(diào)整顏色“條帶”的轉(zhuǎn)角。

image

如果將Curvature(曲率)值調(diào)為0 %,就得到下圖的效果?!爸钡摹币膊诲e(cuò),嗯,我還是覺(jué)得“彎的”比較好看。

image

曲率這里改回默認(rèn)的30 %,換一下配色,最終可得到多種效果,如下:

image

個(gè)人都試了一下,origin最好,可視化操作,不用寫(xiě)代碼,而且相當(dāng)好看

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請(qǐng)通過(guò)簡(jiǎn)信或評(píng)論聯(lián)系作者。

友情鏈接更多精彩內(nèi)容