jfq3 / ggordiplots

Make ggplot Versions of Vegan's Ordiplots
GNU General Public License v2.0
6 stars 2 forks source link

Add family argument to gg_ordisurf #9

Closed seanhardison1 closed 2 years ago

seanhardison1 commented 3 years ago

Hi! Thanks for making such a helpful package. It would be nice to have a family argument in gg_ordisurf to allow vegan::ordisurf to take different response families. This seems to work on my end -

gg_ordisurf <- 
  function (ord, env.var, groups = NA, choices = c(1, 2), var.label = "Level", 
          binwidth, pt.size = 3, plot = TRUE, family = "gaussian") 
{
  groups <- as.factor(groups)
  ordi <- vegan::ordisurf(ord ~ env.var, plot = FALSE, family = family)
  ordi.grid <- ordi$grid
  ordi.data <- expand.grid(x = ordi.grid$x, y = ordi.grid$y)
  ordi.data$z <- as.vector(ordi.grid$z)
  df_surf <- data.frame(na.omit(ordi.data))
  df_ord <- as.data.frame(scores(ord, choices = choices, display = "sites"))
  if (is.na(groups)[1]) {
    df_ord <- data.frame(x = df_ord[, 1], y = df_ord[, 2])
  }
  else {
    df_ord <- data.frame(x = df_ord[, 1], y = df_ord[, 2], 
                         Group = groups)
  }
  axis.labels <- ord_labels(ord)[choices]
  xlab <- axis.labels[1]
  ylab <- axis.labels[2]
  if (missing(binwidth)) {
    r <- range(env.var)
    binwidth <- (r[2] - r[1])/15
  }
  if (is.na(groups)[1]) {
    plt <- ggplot() + geom_point(data = df_ord, aes(x = x, 
                                                    y = y), size = pt.size) + xlab(xlab) + ylab(ylab) + 
      stat_contour(data = df_surf, aes(x = x, y = y, z = z, 
                                       color = ..level..), binwidth = binwidth) + labs(color = var.label) + 
      coord_fixed(ratio = 1)
  }
  else {
    plt <- ggplot() + geom_point(data = df_ord, aes(x = x, 
                                                    y = y, fill = Group), shape = 21, color = "#00000000", 
                                 size = pt.size) + xlab(xlab) + ylab(ylab) + stat_contour(data = df_surf, 
                                                                                          aes(x = x, y = y, z = z, color = ..level..), binwidth = binwidth) + 
      labs(color = var.label) + coord_fixed(ratio = 1)
  }
  if (plot) {
    print(plt)
  }
  invisible(list(df_ord = df_ord, df_surf = df_surf, plot = plt))
}
jfq3 commented 3 years ago

Thanks for the suggestion, Sean. I am on vacation now, but will see about updating my package when I get home. JQ VIMS, class of ‘76

From: Sean Hardison @.> Sent: Wednesday, July 7, 2021 2:10 PM To: jfq3/ggordiplots @.> Cc: Subscribed @.***> Subject: [jfq3/ggordiplots] Add family argument to gg_ordisurf (#9)

Hi! Thanks for making such a helpful package. It would be nice to have a family argument in gg_ordisurf to allow vegan::ordisurf to take different response families. This seems to work on my end -

gg_ordisurf <-

function (ord, env.var, groups = NA, choices = c(1, 2), var.label = "Level",

      binwidth, pt.size = 3, plot = TRUE, family = "gaussian")

{

groups <- as.factor(groups)

ordi <- vegan::ordisurf(ord ~ env.var, plot = FALSE, family = family)

ordi.grid <- ordi$grid

ordi.data <- expand.grid(x = ordi.grid$x, y = ordi.grid$y)

ordi.data$z <- as.vector(ordi.grid$z)

df_surf <- data.frame(na.omit(ordi.data))

df_ord <- as.data.frame(scores(ord, choices = choices, display = "sites"))

if (is.na(groups)[1]) {

df_ord <- data.frame(x = df_ord[, 1], y = df_ord[, 2])

}

else {

df_ord <- data.frame(x = df_ord[, 1], y = df_ord[, 2],

                     Group = groups)

}

axis.labels <- ord_labels(ord)[choices]

xlab <- axis.labels[1]

ylab <- axis.labels[2]

if (missing(binwidth)) {

r <- range(env.var)

binwidth <- (r[2] - r[1])/15

}

if (is.na(groups)[1]) {

plt <- ggplot() + geom_point(data = df_ord, aes(x = x,

                                                y = y), size = pt.size) + xlab(xlab) + ylab(ylab) +

  stat_contour(data = df_surf, aes(x = x, y = y, z = z,

                                   color = ..level..), binwidth = binwidth) + labs(color = var.label) +

  coord_fixed(ratio = 1)

}

else {

plt <- ggplot() + geom_point(data = df_ord, aes(x = x,

                                                y = y, fill = Group), shape = 21, color = "#00000000",

                             size = pt.size) + xlab(xlab) + ylab(ylab) + stat_contour(data = df_surf,

                                                                                      aes(x = x, y = y, z = z, color = ..level..), binwidth = binwidth) +

  labs(color = var.label) + coord_fixed(ratio = 1)

}

if (plot) {

print(plt)

}

invisible(list(df_ord = df_ord, df_surf = df_surf, plot = plt))

}

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://urldefense.com/v3/__https:/github.com/jfq3/ggordiplots/issues/9__;!!HXCxUKc!nxzZlbsEx44YXQJlqLjE2Blljahk0mGxNTcilFHn7t-amOcKvQTmDygywpY0vrU6$, or unsubscribehttps://urldefense.com/v3/__https:/github.com/notifications/unsubscribe-auth/ACAD3P4V7BEGYK4B4MZVDSTTWSJ73ANCNFSM477E7T2A__;!!HXCxUKc!nxzZlbsEx44YXQJlqLjE2Blljahk0mGxNTcilFHn7t-amOcKvQTmDygywqmwTS3n$.

jfq3 commented 2 years ago

Sean, Do you have data for which you preferred a non-gaussian fit?

jfq3 commented 2 years ago

Family argument added to version 4.1

seanhardison1 commented 2 years ago

Excellent! Thanks for the update.