此文內(nèi)容首發(fā)于微信公眾號:R語言搬運工倒得,關(guān)注公眾號瀏覽更多精彩內(nèi)容
**原文鏈接
在科研論文中,經(jīng)掣嫠剑看到有繪圖后根據(jù)分組再點圖外圍加一個圓圈或者多邊形,這樣的圖怎樣實現(xiàn)呢承桥?
如下圖:
圖片源自文獻(xiàn)Identifying sagittal otoliths of Mediterranean Sea gobies:variability among phylogenetic lineages
圖片源自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ù)點的分布模式,使用分組變量將不同分組的點分別使用圓圈和多邊形進(jìn)行frame凶异。其實有多種方法實現(xiàn)這種圖的繪制蜀撑。
剛開始小編在繪制主成分分析(PCA)散點圖的時候嘗試過使用繪圖軟件,比如illustrator剩彬、PS手動畫過酷麦,然而后邊發(fā)現(xiàn)這個圈圈是有統(tǒng)計意義的,不是隨隨便便畫幾個圈圈就能解決問題喉恋,所以建議不要手動畫沃饶。那么我們直接方法2吧!
這里使用常用的鳶尾花數(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ū)間水平
根據(jù)0.9的置信區(qū)間水平添加的點圈轻黑,可以根據(jù)自己的需求調(diào)節(jié)置信水平糊肤,將盡量多的點囊括進(jìn)來
上圖是設(shè)置了置信區(qū)間水平0.99
是不是比較簡單,當(dāng)然還有其他點圈形式氓鄙,在不同的科研論文中均有所使用馆揉,這里簡單介紹幾種:
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()
在包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:圈的外部擴(kuò)展
○ spread: 延申程度
○ s_shape:線條平滑程度
當(dāng)然舷暮,有時候我們更希望是將不同分組的邊界點連起來圍成一個多邊形,這也在R中實現(xiàn)噩茄。
將代碼中的參數(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)
還可以根據(jù)點坐標(biāo)將邊界點提取出來下面,然后使用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"))
寫在文末
以上是此文分享全部內(nèi)容,喜歡點點關(guān)注吧巢墅!
此文內(nèi)容首發(fā)于微信公眾號:R語言搬運工诸狭,關(guān)注公眾號瀏覽更多精彩內(nèi)容
精彩推薦:
R語言繪制散點圖geom_point
R語言添加擬合曲線geom_smooth
R語言箱線圖boxplot
R語言線圖geom_line