蜜蜂圖(beeswarm plot)是一種與“抖動圖”相似的散點圖聊替,這種圖可以直觀地展現(xiàn)每一個樣本的具體情況及整體的情況按灶。
安裝肺孤、加載R包
#安裝包
install.packages("beeswarm")
#加載包
library(beeswarm)
數(shù)據(jù)
set.seed(12)
df<-data.frame(
x=rnorm(1000),
group1=sample(c("G1", "G2", "G3"),
size = 100, replace = TRUE),
group2=as.numeric(factor(sample(c("Yes", "No"),
size = 100, replace = TRUE))))
head(df)#預覽數(shù)據(jù)
繪圖
1眶拉、基礎繪圖
beeswarm(df$x~df$group1)
2千埃、個性化繪制
beeswarm(df$x~df$group1,#指定數(shù)據(jù)
pch=19,#指定點的形狀
col=c("#3FA0FF", "#FFE099", "#F76D5E"),#指定顏色
method = "swarm",#指定數(shù)據(jù)點排列方式,可選擇"swarm"忆植、"center"放可、"hex"、"square"
corral = "gutter",#調(diào)整數(shù)據(jù)點以防止組數(shù)據(jù)重疊朝刊,可選擇有"none"耀里、"gutter"、"wrap"拾氓、"random"冯挎、"omit"
side=0,#位置蜂群顯示方向,有-1咙鞍,0房官,1可選擇
priority = "descending"#布局順序,可選擇"descending"续滋、"random"翰守、"density"、"none"
)
3疲酌、加入子群體
beeswarm(df$x~df$group1,#指定數(shù)據(jù)
pch=19,#點的形狀
pwcol=df$group2,#指定子群體
method = "swarm",#指定數(shù)據(jù)點排列方式蜡峰,可選擇"swarm"、"center"徐勃、"hex"事示、"square"
corral = "gutter",#調(diào)整數(shù)據(jù)點以防止組數(shù)據(jù)重疊,可選擇有"none"僻肖、"gutter"肖爵、"wrap"、"random"臀脏、"omit"
side=0,#位置蜂群顯示方向劝堪,有-1冀自,0,1可選擇
priority = "descending"#布局順序秒啦,可選擇"descending"熬粗、"random"、"density"余境、"none"
)
#圖例
legend("topright", legend = c("Yes", "No"),
col = 1:2, pch = 19)