umerijaz / microbiomeSeq

An R package for microbial community analysis in an environmental context
66 stars 26 forks source link

plot_odisurf Missing #19

Open kalen-rasmussen opened 6 years ago

kalen-rasmussen commented 6 years ago

Hello,

I am trying to run the plot_ordisurf function function, however it doesn't seem to exist within the downloaded R package. however it doesn't seem to exist in the current version of the package. I tried to build out the function using the source code:

plot_ordisurf <- function(sol, meta_table, env.variable, grouping_column){

  groups <- meta_table[,grouping_column] #get grouping information from meta data

  df=data.frame(x=sol$point[,1],y=sol$point[,2],Groups=groups)
  #Add a dummy variable corrresponding to the selected variable
  meta_table$var <- meta_table[,env.variable]

  #fit a surface for a selected variable onto ordination stats
  ordi<- vegan::ordisurf(sol,meta_table$var ,plot = FALSE, bs="ds")
  ordi.grid <- ordi$grid #extracts the ordisurf object
  #str(ordi.grid) #it's a list though - cannot be plotted as is
  ordi.mite <- expand.grid(x = ordi.grid$x, y = ordi.grid$y) #get x and ys
  ordi.mite$z <- as.vector(ordi.grid$z) #unravel the matrix for the z scores
  ordi.mite.na <- data.frame(na.omit(ordi.mite)) #gets rid of the nas

  #make the plot
  p<-ggplot2::ggplot()+stat_contour(data = ordi.mite.na, aes(x = x, y = y, z = z, colour = ..level..),positon="identity") #can change the binwidth depending on how many contours you want
  p<-p+ ggplot2::geom_point(data=df,aes(x,y,fill=Groups),pch=21,size=3)
  p<-p+ ggplot2::scale_colour_continuous(high = "darkgreen", low = "darkolivegreen1") #here we set the high and low of the colour scale.  Can delete to go back to the standard blue, or specify others
  p<-p+ ggplot2::labs(colour = paste(env.variable)) #another way to set the labels, in this case, for the colour legend
  p<-p+ ggplot2::theme_bw()
  return(p)
}

However, after trying to use the function as:

ord.res2 <- ordination(SMU.BA.Rare, which_distance = "bray", method = "NMDS", grouping_column = "Distance", pvalue.cutoff = 0.05)
p <- plot_ordisurf(sol = ord.res2, meta_table = SMU.BA.Rare.meta, env.variable = 'Temperature', grouping_column = "Distance")

I keep getting the following error: Error in scores.default(x, choices = choices, display = display, ...) : cannot find scores

So I then tried to sub set the ordination data do only contain the solution data, so the code could find the points df and ran it again as:

ord.res2 <- ordination(SMU.BA.Rare, which_distance = "bray", method = "NMDS", grouping_column = "Distance", pvalue.cutoff = 0.05)
ord.res2.sol <- ord.res2$solution
p <- plot_ordisurf(sol = ord.res2.sol, meta_table = SMU.BA.Rare.meta, env.variable = 'Temperature', grouping_column = "Distance")
print(p)

Only to receive this error, which I have not been able to figure out. since the solution$points df is a 70x2 df and should have the same number of rows.

Error in data.frame(x = sol$points[, 1], y = sol$points[, 2], Groups = groups) : arguments imply differing number of rows: 70, 0

any help would be much appreciated, -K

Marieag commented 5 years ago

I'm having this issue as well - any news?

AlfredUg commented 5 years ago

Heya @kalen-rasmussen and @Marieag,

plot_ordisurf function is now available for usage as part of the package.

Using @kalen-rasmussen's example above, this should work fine. This will soon be updated in the tutorial.

ord.res2 <- ordination(SMU.BA.Rare, which_distance = "bray", method = "NMDS", grouping_column = "Distance", pvalue.cutoff = 0.05)
ord.res2.sol <- ord.res2$solution
p <- plot_ordisurf(sol = ord.res2.sol, meta_table = SMU.BA.Rare.meta, env.variable = 'Temperature', grouping_column = "Distance")
print(p)

@kalen-rasmussen, kindly note that, you may want to play around with the p-value threshold to get rid of the error reported earlier.

kalen-rasmussen commented 5 years ago

Great, thank you!

giriarteS commented 5 years ago

I am getting a the following error with plot_ordisurf:

meta_table <- sample_data(ps) ord.res2 <- ordination(psN, which_distance = "bray", method = "NMDS", grouping_column = "Treatment", pvalue.cutoff = 0.05) ord.res2.sol <- ord.res2$solution p <- plot_ordisurf(sol = ord.res2.sol, meta_table = meta_table, env.variable = 'Total_C', grouping_column = "Treatment") print(p)

Error in model.frame.default(formula = y ~ 1 + x1 + x2, weights = w, drop.unused.levels = TRUE) : invalid type (list) for variable 'y'

kkerns85 commented 5 years ago

I am getting a the following error with plot_ordisurf:

meta_table <- sample_data(ps) ord.res2 <- ordination(psN, which_distance = "bray", method = "NMDS", grouping_column = "Treatment", pvalue.cutoff = 0.05) ord.res2.sol <- ord.res2$solution p <- plot_ordisurf(sol = ord.res2.sol, meta_table = meta_table, env.variable = 'Total_C', grouping_column = "Treatment") print(p)

Error in model.frame.default(formula = y ~ 1 + x1 + x2, weights = w, drop.unused.levels = TRUE) : invalid type (list) for variable 'y'

I am having the same issue as @giriarteS ... There seems to be lots of issues for me trying to complete the workflow both with the pitlatrine data and my own. :/

javiercnav commented 5 years ago

Has anyone solved the error: Error in model.frame.default(formula = y ~ 1 + x1 + x2, weights = w, drop.unused.levels = TRUE) : invalid type (list) for variable 'y'