r - What the solution for this ggplot2 error on gigINEXT function:" Error in levels<-..."? -
i'm running examples inext
package on rstudio , having trouble graphical example. possible suggestions, below initial steps run without major problems, followed command line generates error:
#packages , dependencies >install.packages("inext") >install.packages('devtools') >library(devtools) >install_github('johnsonhsieh/inext') >library(inext) >library(ggplot2) #example dataset >data(spider) #last line runs before error >out <- inext(spider, q=c(0, 1, 2), datatype="abundance", endpoint=500) #here comes problematic line , error >gginext(out, type=1, facet.var="site") error in `levels<-`(`*tmp*`, value = if (nl == nl) as.character(labels) else paste0(labels, : factor level [2] duplicated
any idea how solve this?
i solved it, adding unique function, inside gginext.inext function: below original command line , same line altered:
original command line:
z$lty <- factor(z$method, c("interpolated", "observed", "extrapolated"), c("interpolation", "interpolation", "extrapolation"))
changed command line:
z$lty <- factor(z$method, levels=unique(c("interpolated", "observed", "extrapolated"), c("interpolation", "interpolation", "extrapolation")))
after change, make graph example https://cran.r-project.org/web/packages/inext/vignettes/introduction.html
just note, used traceback() find problem inside gginext.inext. found command indicated error message, , use unique function based on similar problem post: ggplot: order of factors duplicate levels
to run , construct graphs, past altered function on r console:
gginext.inext<-function (x, type = 1, se = true, facet.var = "none", color.var = "site", grey = false) { type <- c(1, 2, 3) split <- c("none", "order", "site", "both") if (is.na(pmatch(type, type)) | pmatch(type, type) == -1) stop("invalid plot type") if (is.na(pmatch(facet.var, split)) | pmatch(facet.var, split) == -1) stop("invalid facet variable") if (is.na(pmatch(color.var, split)) | pmatch(color.var, split) == -1) stop("invalid color variable") type <- pmatch(type, 1:3) facet.var <- match.arg(facet.var, split) color.var <- match.arg(color.var, split) if (facet.var == "order") color.var <- "site" if (facet.var == "site") color.var <- "order" options(warn = -1) z <- fortify(x, type = type) options(warn = 0) if (ncol(z) == 7) { se <- false } datatype <- unique(z$datatype) if (color.var == "none") { if (levels(factor(z$order)) > 1 & "site" %in% names(z)) { warning("invalid color.var setting, inext object consists multiple sites , orders, change setting both") color.var <- "both" z$col <- z$shape <- paste(z$site, z$order, sep = "-") } else if ("site" %in% names(z)) { warning("invalid color.var setting, inext object consists multiple orders, change setting order") color.var <- "site" z$col <- z$shape <- z$site } else if (levels(factor(z$order)) > 1) { warning("invalid color.var setting, inext object consists multiple sites, change setting site") color.var <- "order" z$col <- z$shape <- factor(z$order) } else { z$col <- z$shape <- rep(1, nrow(z)) } } else if (color.var == "order") { z$col <- z$shape <- factor(z$order) } else if (color.var == "site") { if (!"site" %in% names(z)) { warning("invalid color.var setting, inext object not consist multiple sites, change setting order") z$col <- z$shape <- factor(z$order) } z$col <- z$shape <- z$site } else if (color.var == "both") { if (!"site" %in% names(z)) { warning("invalid color.var setting, inext object not consist multiple sites, change setting order") z$col <- z$shape <- factor(z$order) } z$col <- z$shape <- paste(z$site, z$order, sep = "-") } z$lty <- factor(z$method, levels=unique(c("interpolated", "observed", "extrapolated"), c("interpolation", "interpolation", "extrapolation"))) z$col <- factor(z$col) data.sub <- z[which(z$method == "observed"), ] g <- ggplot(z, aes_string(x = "x", y = "y", colour = "col")) + geom_point(aes_string(shape = "shape"), size = 5, data = data.sub) g <- g + geom_line(aes_string(linetype = "lty"), lwd = 1.5) + guides(linetype = guide_legend(title = "method"), colour = guide_legend(title = "guides"), fill = guide_legend(title = "guides"), shape = guide_legend(title = "guides")) + theme(legend.position = "bottom", legend.title = element_blank(), text = element_text(size = 18)) if (type == 2l) { g <- g + labs(x = "number of sampling units", y = "sample coverage") if (datatype == "abundance") g <- g + labs(x = "number of individuals", y = "sample coverage") } else if (type == 3l) { g <- g + labs(x = "sample coverage", y = "species diversity") } else { g <- g + labs(x = "number of sampling units", y = "species diversity") if (datatype == "abundance") g <- g + labs(x = "number of individuals", y = "species diversity") } if (se) g <- g + geom_ribbon(aes_string(ymin = "y.lwr", ymax = "y.upr", fill = "factor(col)", colour = "null"), alpha = 0.2) if (facet.var == "order") { if (length(levels(factor(z$order))) == 1 & type != 2) { warning("invalid facet.var setting, inext object not consist multiple orders.") } else { g <- g + facet_wrap(~order, nrow = 1) if (color.var == "both") { g <- g + guides(colour = guide_legend(title = "guides", ncol = length(levels(factor(z$order))), byrow = true), fill = guide_legend(title = "guides")) } } } if (facet.var == "site") { if (!"site" %in% names(z)) { warning("invalid facet.var setting, inext object not consist multiple sites.") } else { g <- g + facet_wrap(~site, nrow = 1) if (color.var == "both") { g <- g + guides(colour = guide_legend(title = "guides", nrow = length(levels(factor(z$order)))), fill = guide_legend(title = "guides")) } } } if (facet.var == "both") { if (length(levels(factor(z$order))) == 1 | !"site" %in% names(z)) { warning("invalid facet.var setting, inext object not consist multiple sites or orders.") } else { g <- g + facet_wrap(site ~ order) if (color.var == "both") { g <- g + guides(colour = guide_legend(title = "guides", nrow = length(levels(factor(z$site))), byrow = true), fill = guide_legend(title = "guides")) } } } if (grey) { g <- g + theme_bw(base_size = 18) + scale_fill_grey(start = 0, end = 0.4) + scale_colour_grey(start = 0.2, end = 0.2) + guides(linetype = guide_legend(title = "method"), colour = guide_legend(title = "guides"), fill = guide_legend(title = "guides"), shape = guide_legend(title = "guides")) + theme(legend.position = "bottom", legend.title = element_blank()) } g <- g + theme(legend.box = "vertical") return(g) }
Comments
Post a Comment