R語言繪圖基礎(chǔ)篇-PCA加置信圈

此文內(nèi)容首發(fā)于微信公眾號:R語言搬運工,關(guān)注公眾號瀏覽更多精彩內(nèi)容

**原文鏈接

image.png

在科研論文中,經(jīng)??吹接欣L圖后根據(jù)分組再點圖外圍加一個圓圈或者多邊形,這樣的圖怎樣實現(xiàn)呢?

如下圖:


image.png

圖片源自文獻Identifying sagittal otoliths of Mediterranean Sea gobies:variability among phylogenetic lineages


image.png

圖片源自O(shè)tolith shape analysis as a tool to infer the population structure of the blue jack mackerel, Trachurus picturatus, in the NE Atlantic
上兩圖主要用到了CAP和CVA分析得到的樣點空間分布圖,根據(jù)點的分布模式,使用分組變量將不同分組的點分別使用圓圈和多邊形進行frame。其實有多種方法實現(xiàn)這種圖的繪制。
image.png

剛開始小編在繪制主成分分析(PCA)散點圖的時候嘗試過使用繪圖軟件,比如illustrator、PS手動畫過,然而后邊發(fā)現(xiàn)這個圈圈是有統(tǒng)計意義的,不是隨隨便便畫幾個圈圈就能解決問題,所以建議不要手動畫。那么我們直接方法2吧!


image.png

image.png

這里使用常用的鳶尾花數(shù)據(jù)作為示例(這個數(shù)據(jù)余熱好燙)


library(ggplot2)
iris_pca <- princomp(formula = ~Sepal.Length + Sepal.Width + Petal.Length + Petal.Width,
                     data = iris)
# 添加橢圓
ggplot(iris_pca$scores, aes(x = Comp.1, y = Comp.2,color=iris$Species)) +
  geom_point() +
  stat_ellipse(level = 0.9)

○ level:置信區(qū)間水平


image.png

根據(jù)0.9的置信區(qū)間水平添加的點圈,可以根據(jù)自己的需求調(diào)節(jié)置信水平,將盡量多的點囊括進來


image.png

上圖是設(shè)置了置信區(qū)間水平0.99
是不是比較簡單,當然還有其他點圈形式,在不同的科研論文中均有所使用,這里簡單介紹幾種:

df<-iris
colnames(df)<-paste0(
  "V"
  ,1:5)
library(ggplot2)
library(ggforce)
ggplot(data=df,aes(x=V1,y=V2,color=V5))+
  geom_point()+
  geom_mark_rect(aes(fill=V5),alpha=0.1)+
  theme_bw()
image.png

image.png

在包ggalt中封裝了實現(xiàn)根據(jù)點分布形狀繪制點圈的函數(shù),可視化效果更好,這里簡單介紹一下。

library(ggalt)

princomp(formula = ~Sepal.Length + Sepal.Width + Petal.Length + Petal.Width,
         data = iris) %>%
  fortify() %>%
  add_column(FactorSpecies = factor(iris$Species)) %>%
  ggplot(aes(x = Comp.1, y = Comp.2, color = FactorSpecies)) +
  geom_point() +
  scale_x_continuous(limits=c(-5,5))+
  scale_y_continuous(limits=c(-2,2))+
  geom_encircle(aes(group = FactorSpecies),expand=0.1,spread=0.5,s_shape=0.9)

對代碼中重要參數(shù)做簡單的介紹:

                  ○  expand:圈的外部擴展

                          ○  spread: 延申程度

                          ○  s_shape:線條平滑程度
image.png

當然,有時候我們更希望是將不同分組的邊界點連起來圍成一個多邊形,這也在R中實現(xiàn)。


image.png

將代碼中的參數(shù)expand設(shè)置為0即可實現(xiàn):

library(ggalt)

princomp(formula = ~Sepal.Length + Sepal.Width + Petal.Length + Petal.Width,
         data = iris) %>%
  fortify() %>%
  add_column(FactorSpecies = factor(iris$Species)) %>%
  ggplot(aes(x = Comp.1, y = Comp.2, color = FactorSpecies)) +
  geom_point() +
  geom_encircle(aes(group = FactorSpecies),expand=0,spread=0.5,s_shape=0.9)

image.png

還可以根據(jù)點坐標將邊界點提取出來,然后使用geom_polygon函數(shù)添加邊界

iris_pca <- princomp(formula = ~Sepal.Length + Sepal.Width + Petal.Length + Petal.Width,
                     data = iris)
iris_scores <- cbind(as.data.frame(iris_pca$scores[,1:2]),iris$Species)

time.a <- iris_scores[iris_scores$`iris$Species` == "setosa",][chull(iris_scores[iris_scores$`iris$Species` == "setosa", c("Comp.1", "Comp.2")]),] # hull values for time A
time.b <- iris_scores[iris_scores$`iris$Species` == "versicolor",][chull(iris_scores[iris_scores$`iris$Species` == "versicolor", c("Comp.1", "Comp.2")]),] # hull values for time A
time.c <- iris_scores[iris_scores$`iris$Species` == "virginica",][chull(iris_scores[iris_scores$`iris$Species` == "virginica", c("Comp.1", "Comp.2")]),] # hull values for time A
hull.data.time <- rbind(time.a, time.b,time.c) #combine grp.a and grp.b
ggplot()+
  geom_polygon(data=hull.data.time,aes(x=Comp.1,y=Comp.2,color=`iris$Species`),linetype="dashed",alpha=0.2) + # add the convex hulls
  geom_point(data=iris_scores,aes(x=Comp.1,y=Comp.2,shape=`iris$Species`,color=`iris$Species`),size=4) + # add the point markers
  scale_fill_manual(values=c("white","white","white"))
image.png

寫在文末

以上是此文分享全部內(nèi)容,喜歡點點關(guān)注吧!

此文內(nèi)容首發(fā)于微信公眾號:R語言搬運工,關(guān)注公眾號瀏覽更多精彩內(nèi)容

精彩推薦:

R語言繪制散點圖geom_point
R語言添加擬合曲線geom_smooth
R語言箱線圖boxplot
R語言線圖geom_line

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

相關(guān)閱讀更多精彩內(nèi)容

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