如何对一系列变量进行箱形图

时间:2021-02-06 12:05:46

I have a data matrix with approximately one hundred variables and I want to do box plots of these variables. Doing them one by one is possible, but tedious. The code I use for my box plots is:

我有一个大约有一百个变量的数据矩阵,我想做这些变量的箱形图。一个接一个地做它们是可能的,但是很乏味。我用于箱形图的代码是:

    boxplot(myVar ~ Group*Trt*Time,data=exp,col=c('red','blue'),frame.plot=T,las=2, ylab='Counts', at=c(1,2,3,4,6,7,8,9,11,12,13,14,16,17,18,19))

I started doing them one by one, but realized there must be better options. So, the boxplot call will take only one variable at at time (I may be wrong), so I am looking for a way to get it done in one go. A for loop? Next, I would like to print the name of the current variable (= the colName) on the plot in order to keep them apart.

我开始一个接一个地做,但意识到必须有更好的选择。因此,boxplot调用在时间上只占用一个变量(我可能是错的),所以我正在寻找一种方法来一次完成它。一个for循环?接下来,我想在绘图上打印当前变量(= colName)的名称,以使它们分开。

Appreciate suggestions. Thank you. jd

赞赏建议。谢谢。 JD

2 个解决方案

#1


1  

Why not try the following:

为什么不尝试以下方法:

data(something)
panel.bxp <- function(x, ...)
{
    a <- par("a"); on.exit(par(a))
    par(a = c(0, 2, a[3:4]))
    boxplot(x, add=TRUE)
}

Then, to run the function, you can try something like the following:

然后,要运行该函数,您可以尝试以下内容:

pairs(something, diag.panel = panel.bxp, text.panel = function(...){})

EDIT: There is also a nice link to an article here on R-bloggers which you might want to have a look at.

编辑:还有一个很好的链接,这里有关于R-bloggers的文章,你可能想看看。

#2


0  

Being very new to R, I've tried to follow my 'old' thinking - making a for-loop. Here is what I came up with. Probably very primitive, and therefore, I'd appreciate comments/suggestions. Anyway: the loop:

作为R的新手,我试图遵循我的“旧”思想 - 制作一个for循环。这就是我想出的。可能非常原始,因此,我很感激评论/建议。无论如何:循环:

    for (i in 1:ncol(final)) {
      #print(i)
      c <- colnames(final)[i]
      #print(c)
      b <- final[,i]
      #b <- t(b)
      #dim(b)
      #print(b)
      exp <- data.frame(Group,Trt,Time,b)
      #dim(exp)
      #print(exp)
      boxplot(b ~ Group*Trt*Time,data=exp,col=c('red','blue'),frame.plot=T, las=2, ylab='Counts',main=c, at=c(1,2,3,4,6,7,8,9,11,12,13,14,16,17,18,19))

    }

The loop runs through the data matrix 'final', (48rows x 67cols). Picks up the column header, c, which is used in the boxplot call as main title. Picks up the data column, b. Sets up the experiment using the Group, Trt, and Time factors established outside the loop, and calls the boxplot. This seem to do what I want. Oddly, Rstudio does not allow more than 25 (approx) plots to be stored in the plots console, so I have to run this loop in a couple of rounds. Anyway, sorry for answering my own question. Better solutions are greatly appreciated since my way is pretty amateourish, I suspect.

循环遍历数据矩阵'final',(48rows x 67cols)。拾取列标题c,它在boxplot调用中用作主标题。拿起数据列,b。使用在循环外部建立的Group,Trt和Time因子设置实验,并调用boxplot。这似乎做我想要的。奇怪的是,Rstudio不允许将多于25个(大约)的图存储在图控制台中,因此我必须在几轮中运行此循环。无论如何,抱歉回答我自己的问题。我怀疑,更好的解决方案非常受欢迎,因为我的方式很漂亮。

#1


1  

Why not try the following:

为什么不尝试以下方法:

data(something)
panel.bxp <- function(x, ...)
{
    a <- par("a"); on.exit(par(a))
    par(a = c(0, 2, a[3:4]))
    boxplot(x, add=TRUE)
}

Then, to run the function, you can try something like the following:

然后,要运行该函数,您可以尝试以下内容:

pairs(something, diag.panel = panel.bxp, text.panel = function(...){})

EDIT: There is also a nice link to an article here on R-bloggers which you might want to have a look at.

编辑:还有一个很好的链接,这里有关于R-bloggers的文章,你可能想看看。

#2


0  

Being very new to R, I've tried to follow my 'old' thinking - making a for-loop. Here is what I came up with. Probably very primitive, and therefore, I'd appreciate comments/suggestions. Anyway: the loop:

作为R的新手,我试图遵循我的“旧”思想 - 制作一个for循环。这就是我想出的。可能非常原始,因此,我很感激评论/建议。无论如何:循环:

    for (i in 1:ncol(final)) {
      #print(i)
      c <- colnames(final)[i]
      #print(c)
      b <- final[,i]
      #b <- t(b)
      #dim(b)
      #print(b)
      exp <- data.frame(Group,Trt,Time,b)
      #dim(exp)
      #print(exp)
      boxplot(b ~ Group*Trt*Time,data=exp,col=c('red','blue'),frame.plot=T, las=2, ylab='Counts',main=c, at=c(1,2,3,4,6,7,8,9,11,12,13,14,16,17,18,19))

    }

The loop runs through the data matrix 'final', (48rows x 67cols). Picks up the column header, c, which is used in the boxplot call as main title. Picks up the data column, b. Sets up the experiment using the Group, Trt, and Time factors established outside the loop, and calls the boxplot. This seem to do what I want. Oddly, Rstudio does not allow more than 25 (approx) plots to be stored in the plots console, so I have to run this loop in a couple of rounds. Anyway, sorry for answering my own question. Better solutions are greatly appreciated since my way is pretty amateourish, I suspect.

循环遍历数据矩阵'final',(48rows x 67cols)。拾取列标题c,它在boxplot调用中用作主标题。拿起数据列,b。使用在循环外部建立的Group,Trt和Time因子设置实验,并调用boxplot。这似乎做我想要的。奇怪的是,Rstudio不允许将多于25个(大约)的图存储在图控制台中,因此我必须在几轮中运行此循环。无论如何,抱歉回答我自己的问题。我怀疑,更好的解决方案非常受欢迎,因为我的方式很漂亮。