不同长度向量的箱线图 [英] boxplot of vectors with different length

查看:208
本文介绍了不同长度向量的箱线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2列矩阵。我想对这些列中的每一个进行箱线绘图,但是每个列具有不同的条目数。

I have a matrix of 2 columns. I would like boxplot each of these columns but each has different number of entries.

例如,第一列有10个条目,第二列有7个条目。第二列的其余3个为零。

For example, first column has 10 entries and the second column has 7 entries. The remaining 3 of the second column is given zero.

出于比较的原因,我想并排绘制这些图形。

I would like to plot these side by side for comparison reasons.

是否有一种方法告诉R将整个第1列和第2列的仅前7个条目进行箱图绘制?

Is there a way to tell R to boxplot the whole column 1 and only the first 7 entry for column 2?

推荐答案

您可以简单地索引所需的值,例如

You could simply index the values you want, for example

## dummy version of your data
mat <- matrix(c(1:17, rep(0, 3)), ncol = 2)

## create object suitable for plotting with boxplot
## I.e. convert to melted or long format
df <- data.frame(values = mat[1:17],
                 vars = rep(c("Col1","Col2"), times = c(10,7)))

## draw the boxplot
boxplot(values ~ vars, data = df)

在上面,我想告诉您,您有一个矩阵。如果您确实有数据框,则需要

In the above I'm taking you at your word that you have a matrix. If you actually have a data frame then you would need

df <- data.frame(values = c(mat[,1], mat[1:7, 2]),
                 vars = rep(c("Col1","Col2"), times = c(10,7)))

,我认为两列中的数据具有可比性,因为两列中的值表明存在一个类别变量,该变量允许我们将值进行拆分(例如男女身高,以性别为绝对值)。

and I assume that the data in the two columns are comparable in that the fact that the values are in two columns suggests a categorical variable that allows us to split the values (like Height of men and women, with sex as the categorical value).

结果箱图如下所示

这篇关于不同长度向量的箱线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆