更改段的颜色并根据绘图函数

时间:2017-03-03 09:08:51

标签: r plot colors ellipse centroid

我的数据与此类似,但有更多的组(而不是2组:放牧/未放牧,我有9年):

data(varespec)
dis <- vegdist(varespec) ## Bray-Curtis distances between samples
groups <- factor(c(rep(1,16), rep(2,8)), labels = c("grazed","ungrazed")) ## First 16 sites grazed, remaining 8 sites ungrazed

我正在努力使用以下功能来根据他们的群体(放牧,未驯化)来改变片段的颜色。所以放牧的线条将是黄色3并且未被玷污将成为印第安人。

mod <- betadisper(dis, groups) ## Calculate multivariate dispersions
## plot function
plot.betadisper <- function(x, axes = c(1,2), 
                            cex = 0.7, pch = c(0,1), col=c("yellow1", "indianred1"),
                            hull = TRUE,
                            ylab, xlab, main, sub, ...)
{
  localAxis <- function(..., col, bg, pch, cex, lty, lwd) axis(...)
  localBox <- function(..., col, bg, pch, cex, lty, lwd) box(...)
  localTitle <- function(..., col, bg, pch, cex, lty, lwd) title(...)
  if(missing(main))
    main <- deparse(substitute(x))
  if(missing(sub))
    sub <- paste("method = \"", attr(x, "method"), "\"", sep = "")
  if(missing(xlab))
    xlab <- paste("PCoA", axes[1])
  if(missing(ylab))
    ylab <- paste("PCoA", axes[2])
  g <- scores(x, choices = axes)
  ng <- length(levels(x$group))
  col <- rep_len(col, ng)  # make sure there are enough colors
  plot(g$sites, asp = 1, type = "n", axes = FALSE, ann = FALSE, ...)
  ## if more than 1 group level
  if(is.matrix(g$centroids)) {
    for(i in levels(x$group)) {
      j <- which(levels(x$group) == i)
      segments(g$centroids[j, 1L], g$centroids[j, 2L],
               g$sites[x$group == i, 1L],
               g$sites[x$group == i, 2L], col = "grey", ...)
      if(hull) {
        ch <- chull(g$sites[x$group == i, ])
        ch <- c(ch, ch[1])
        lines(x$vectors[x$group == i, axes][ch, ],
              col = "black", lty = "dashed", ...)
      }
    }
    points(g$centroids, pch = c(21,22), cex = 1, col = c("yellow3", "indianred3"), bg = c("yellow3", "indianred3"))
  } else {
    ## single group
    segments(g$centroids[1L], g$centroids[2L],
             g$sites[, 1L], g$sites[, 2L], col = "grey", ...)
    if(hull) {
      ch <- chull(g$sites)
      ch <- c(ch, ch[1])
      lines(x$vectors[, axes][ch, ],
            col = "black", lty = "dashed", ...)
    }
    points(g$centroids[1L], g$centroids[1L],
           pch = c(21,22), cex = 1, col = c("yellow3", "indianred3"), bg = c("yellow3", "indianred3"))
  }
  points(g$sites, pch = pch[x$group], cex = cex, col=col[x$group], ...)
  localTitle(main = main, xlab = xlab, ylab = ylab, sub = sub, ...)
  localAxis(1, ...)
  localAxis(2, ...)
  localBox(...)
  class(g) <- "ordiplot"
  invisible(g)
}
plot.betadisper(mod)

我还想添加标准的错误省略号,我在没有找到解决方案的情况下尝试了这个:

plot(mod, hull = FALSE, ellipse = TRUE) ## ??? Not working  

谢谢!

0 个答案:

没有答案