i.b
i.b

Reputation: 337

plot mantel.rtest with ggplot2

I am performing a mantel test using the function mantel.rtest from ade4 on two Euclidean distance matrices to get the correlation between them. Since I would like to show the resulting plot for different tests, I would like to know if it would be possible to plot the mantel result using ggplot2 instead of the basic function plot.

first, of all I have tried to convert r1 into data.frame but I get this error:

r2 <- as.data.frame(r1)
Error in as.data.frame.default(r1) : 
  cannot coerce class ‘c("mantelrtest", "randtest", "lightrandtest")’ to a data.fr

I am adding a reproducible example:

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

Thanks a lot!

Upvotes: 1

Views: 1899

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 173858

The following function will draw a ggplot for your mantelrtest object:

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)
}

So, using your own example:

plot(r1)

enter image description here

ggplot_mantel(r1)

enter image description here

Upvotes: 2

Related Questions