一些代码在SIBRE的捆绑演示数据集上执行此操作 .
在这个例子中,我们尝试使用ggplot2创建后椭圆的多个样本的一些图 .
library(SIBER)
library(ggplot2)
library(dplyr)
library(ellipse)
使基本SIBER模型适合与包捆绑在一起的示例数据 .
# load in the included demonstration dataset
data("demo.siber.data")
#
# create the siber object
siber.example
# Calculate summary statistics for each group: TA, SEA and SEAc
group.ML
# options for running jags
parms
parms$n.iter
parms$n.burnin
parms$n.thin
parms$n.chains
# define the priors
priors
priors$R
priors$k
priors$tau.mu
# fit the ellipses which uses an Inverse Wishart prior
# on the covariance matrix Sigma, and a vague normal prior on the
# means. Fitting is via the JAGS method.
ellipses.posterior
# The posterior estimates of the ellipses for each group can be used to
# calculate the SEA.B for each group.
SEA.B
siberDensityPlot(SEA.B, xticklabels = colnames(group.ML),
xlab = c("Community | Group"),
ylab = expression("Standard Ellipse Area " ('\u2030' ^2) ),
bty = "L",
las = 1,
main = "SIBER ellipses on each group"
)
现在我们想要从这些分布中创建一些样本椭圆的图 . 我们需要为每个组创建所有省略号的data.frame对象 . 在这个例子中,我们简单地将第10个后部绘制假设它们彼此独立,但如果您愿意,可以随机抽取样本 .
# how many of the posterior draws do you want?
n.posts
# decide how big an ellipse you want to draw
p.ell
# for a standard ellipse use
# p.ell
# a list to store the results
all_ellipses
# loop over groups
for (i in 1:length(ellipses.posterior)){
# a dummy variable to build in the loop
ell
post.id
for ( j in 1:n.posts){
# covariance matrix
Sigma
# mean
mu
# ellipse points
out
ell
post.id
}
ell
ell$rep
all_ellipses[[i]]
}
ellipse_df
# now we need the group and community names
# extract them from the ellipses.posterior list
group_comm_names
# split them and conver to a matrix, NB byrow = T
split_group_comm
nrow(ellipse_df), 2, byrow = TRUE)
ellipse_df$community
ellipse_df$group
ellipse_df
现在来创建情节 . 首先根据需要绘制所有原始数据 .
first.plot
geom_point(aes(color = factor(group):factor(community)), size = 2)+
ylab(expression(paste(delta^{15}, "N (\u2030)")))+
xlab(expression(paste(delta^{13}, "C (\u2030)"))) +
theme(text = element_text(size=15))
print(first.plot)
现在我们可以尝试按组添加顶部和刻面上的后椭圆
second.plot
print(second.plot)
# rename columns of ellipse_df to match the aesthetics
third.plot
geom_polygon(data = ellipse_df,
mapping = aes(iso1, iso2,
group = rep,
color = factor(group):factor(community),
fill = NULL),
fill = NA,
alpha = 0.2)
print(third.plot)