使用 ggplot2 绘制 mantel.rtest
plot mantel.rtest with ggplot2
我正在使用 ade4 中的函数 mantel.rtest
对两个欧氏距离矩阵执行 mantel 测试,以获得它们之间的相关性。由于我想显示不同测试的结果图,我想知道是否可以使用 ggplot2 而不是基本函数 plot
.
来绘制壁炉架结果
首先,我尝试将 r1 转换为 data.frame,但出现此错误:
r2 <- as.data.frame(r1)
Error in as.data.frame.default(r1) :
cannot coerce class ‘c("mantelrtest", "randtest", "lightrandtest")’ to a data.fr
我正在添加一个可重现的示例:
data(yanomama)
gen <- quasieuclid(as.dist(yanomama$gen))
geo <- quasieuclid(as.dist(yanomama$geo))
plot(r1 <- mantel.rtest(geo,gen), main = "Mantel's test")
r1
非常感谢!
以下函数将为您的 mantelrtest
对象绘制一个 ggplot:
ggplot_mantel <- function(mant, fill = "gray50") {
df <- data.frame(x = mant$plot$hist$mids,
y = mant$plot$hist$counts)
ggplot(df, aes(x, y)) +
geom_col(orientation = "x",
width = diff(mant$plot$hist$breaks)[1],
fill = fill, color = "gray30") +
labs(x = mant$plot$hist$xname, y = "Frequency") +
scale_x_continuous(limits = mant$plot$xlim) +
geom_segment(aes(x = mant$obs, xend = mant$obs, y = 0,
yend = 0.75 * max(y))) +
geom_point(aes(x = mant$obs, y = 0.75 * max(y)), size = 5,
shape = 18)
}
因此,使用您自己的示例:
plot(r1)
ggplot_mantel(r1)
我正在使用 ade4 中的函数 mantel.rtest
对两个欧氏距离矩阵执行 mantel 测试,以获得它们之间的相关性。由于我想显示不同测试的结果图,我想知道是否可以使用 ggplot2 而不是基本函数 plot
.
首先,我尝试将 r1 转换为 data.frame,但出现此错误:
r2 <- as.data.frame(r1)
Error in as.data.frame.default(r1) :
cannot coerce class ‘c("mantelrtest", "randtest", "lightrandtest")’ to a data.fr
我正在添加一个可重现的示例:
data(yanomama)
gen <- quasieuclid(as.dist(yanomama$gen))
geo <- quasieuclid(as.dist(yanomama$geo))
plot(r1 <- mantel.rtest(geo,gen), main = "Mantel's test")
r1
非常感谢!
以下函数将为您的 mantelrtest
对象绘制一个 ggplot:
ggplot_mantel <- function(mant, fill = "gray50") {
df <- data.frame(x = mant$plot$hist$mids,
y = mant$plot$hist$counts)
ggplot(df, aes(x, y)) +
geom_col(orientation = "x",
width = diff(mant$plot$hist$breaks)[1],
fill = fill, color = "gray30") +
labs(x = mant$plot$hist$xname, y = "Frequency") +
scale_x_continuous(limits = mant$plot$xlim) +
geom_segment(aes(x = mant$obs, xend = mant$obs, y = 0,
yend = 0.75 * max(y))) +
geom_point(aes(x = mant$obs, y = 0.75 * max(y)), size = 5,
shape = 18)
}
因此,使用您自己的示例:
plot(r1)
ggplot_mantel(r1)