如何在单个图中绘制内部使用par-mfrow的函数的不同输出?

时间:2013-11-13 16:21:34

标签: r plot

我正在使用一个功能,在几个方面使用par(mfrow = c(1, 2))在两个面板中绘制结果。

我想运行具有三个不同数据集的函数并将输出绘制在一起,以便每个输出都在一行中,就像我使用par(mfrow = c(3, 2))一样。

如果有办法不修改函数本身吗?

可能是基本问题,但非常感谢。

该功能有点长,但相关部分绘制了一个PCoA:

# perform classical multidimensional sclaing (PCoA) of the dist matrix      
  acop <- cmdscale(dat.d, k = nrow(as.matrix(dat.d)) - 1, eig = TRUE) # keep n-1 eigenvalues

  axes.tot <- acop$eig # eig are the n eigenvalues computed by cmdscale. Axes are ranked by their 
eigenvalues, so the first axis has the highest eigenvalue, the second axis has the second highest 
eigenvalue, etc.

  inertia <- sum(acop$eig[acop$eig > 0]) 

  percents <- round(as.numeric(100 * axes.tot/inertia), digits = 0) # The eigenvalues represent 
the variance extracted by each axis, here they are expressed as a percentage of the sum of all 
eigenvalues (i.e. total variance).

  par()

  par(mfrow = c(1, 2), pty = "s")

  coord1 <- acop$points[, c(1, 2)] # points is a matrix whose rows give the coordinates of the 
points chosen to represent the dissimilarities

  col.grps <- data.frame(vect.grps, coord1)

  # plot so that the maximum variance is projected along the first axis, then on the second and so 
on

  plot(coord1, asp = 1, cex = 0.1, xlab = paste("Axis 1 ", 

    "(", percents[1], " % variance explained)", sep = ""), 

    ylab = paste("Axis 2 ", "(", percents[2], " % variance explained)", 

      sep = ""), main = "", type = "n", bty = "n")

  abline(h = 0, lty = 2, col = "grey")

  abline(v = 0, lty = 2, col = "grey")

  if (length(vect.grps) == nrow(as.matrix(dat.d))) {

    for (g in 1:length(names.grps)) {

      text(x = coord1[col.grps[, 1] == names.grps[g], 1], 

        y = coord1[col.grps[, 1] == names.grps[g], 2], 

        labels = names.grps[g], col = topo.col[g], cex = 0.7)

    }

  }

  else {

    points(coord1, pch = 19, col = "blue", cex = 0.5)

  }


   coord1 <- acop$points[, c(3, 4)]

 col.grps <- data.frame(vect.grps, coord1)

plot(coord1, asp = 1, cex = 0.1, xlab = paste("Axis 3 ", 

  "(", percents[3], " % variance explained)", sep = ""), 

 ylab = paste("Axis 4 ", "(", percents[4], " % variance explained)", 

   sep = ""), main = "", type = "n", bty = "n")

abline(h = 0, lty = 2, col = "grey")

abline(v = 0, lty = 2, col = "grey")

if (length(vect.grps) == nrow(as.matrix(dat.d))) {

 for (g in 1:length(names.grps)) {

   text(x = coord1[col.grps[, 1] == names.grps[g], 1], 

     y = coord1[col.grps[, 1] == names.grps[g], 2], 

     labels = names.grps[g], col = topo.col[g], cex = 0.7)

 }

  }

  else {

    points(coord1, pch = 19, col = "blue", cex = 0.5)

1 个答案:

答案 0 :(得分:2)

我猜你可以覆盖par()

foo <- function(){

  par(mfrow=c(2,1), mar=c(0,0,0,0))
  plot(1:10)
  plot(rnorm(10))

}

par <- function(mfrow, ...) {graphics::par(mfrow=c(3, 2), ...)}
foo()
rm(par)