【问题标题】:plot mantel.rtest with ggplot2用 ggplot2 绘制 mantel.rtest
【发布时间】:2021-02-21 05:23:37
【问题描述】:

我正在使用来自 ade4 的函数 mantel.rtest 对两个欧几里得距离矩阵进行壁炉测试,以获得它们之间的相关性。由于我想展示不同测试的结果图,我想知道是否可以使用 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

非常感谢!

【问题讨论】:

  • 我猜这取决于函数 mantel.rtest 返回的具体内容,运行 str(r1) 并查看是否可以找到创建绘图所需的数据

标签: r ggplot2 correlation


【解决方案1】:

以下函数将为您的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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-07
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    • 2015-07-22
    • 1970-01-01
    相关资源
    最近更新 更多