在一张图中绘制多个箱线图 [英] Plot multiple boxplot in one graph

查看:94
本文介绍了在一张图中绘制多个箱线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将数据保存为具有 12 列的 .csv 文件.第 2 列到第 11 列(标记为 F1, F2, ..., F11)是 features.第一列包含这些特征的label,要么good,要么bad.

I saved my data in as a .csv file with 12 columns. Columns two through 11 (labeled F1, F2, ..., F11) are features. Column one contains the label of these features either good or bad.

我想根据标签绘制所有这11个特征boxplot,但由good分隔> 或 bad.到目前为止,我的代码是:

I would like to plot a boxplot of all these 11 features against the label, but separate by good or bad. My code so far is:

qplot(Label, F1, data=testData, geom = "boxplot", fill=Label, 
          binwidth=0.5, main="Test") + xlab("Label") + ylab("Features")

但是,这仅针对 label 显示 F1.

However, this only shows F1 against the label.

我的问题是:如何在具有一些躲避位置的图表中针对label显示F2, F3, ..., F11?我已经对特征进行了标准化,因此它们在 [0 1] 范围内处于相同的比例.

My question is: How to show F2, F3, ..., F11 against the label in one graph with some dodge position? I have normalized the features so they are in the same scale within [0 1] range.

可以在此处找到测试数据.我已经手绘了一些东西来解释这个问题(见下文).

The test data can be found here. I have drawn something by hand to explain the problem (see below).

推荐答案

在绘图之前,您应该通过融合数据(有关融合数据的外观,请参见下文)以特定格式获取数据.否则,你所做的似乎没问题.

You should get your data in a specific format by melting your data (see below for how melted data looks like) before you plot. Otherwise, what you have done seems to be okay.

require(reshape2)
df <- read.csv("TestData.csv", header=T)
# melting by "Label". `melt is from the reshape2 package. 
# do ?melt to see what other things it can do (you will surely need it)
df.m <- melt(df, id.var = "Label")
> df.m # pasting some rows of the melted data.frame

#     Label variable      value
# 1    Good       F1 0.64778924
# 2    Good       F1 0.54608791
# 3    Good       F1 0.46134200
# 4    Good       F1 0.79421221
# 5    Good       F1 0.56919951
# 6    Good       F1 0.73568570
# 7    Good       F1 0.65094207
# 8    Good       F1 0.45749702
# 9    Good       F1 0.80861929
# 10   Good       F1 0.67310067
# 11   Good       F1 0.68781739
# 12   Good       F1 0.47009455
# 13   Good       F1 0.95859182
# 14   Good       F1 1.00000000
# 15   Good       F1 0.46908343
# 16    Bad       F1 0.57875528
# 17    Bad       F1 0.28938046
# 18    Bad       F1 0.68511766

require(ggplot2)
ggplot(data = df.m, aes(x=variable, y=value)) + geom_boxplot(aes(fill=Label))

我意识到您可能需要分面.这也是一个实现:

I realise that you might need to facet. Here's an implementation of that as well:

p <- ggplot(data = df.m, aes(x=variable, y=value)) + 
             geom_boxplot(aes(fill=Label))
p + facet_wrap( ~ variable, scales="free")

如何添加x-labelsy-labelstitle、更改图例标题,添加一个jitter?

Edit 2: How to add x-labels, y-labels, title, change legend heading, add a jitter?

p <- ggplot(data = df.m, aes(x=variable, y=value)) 
p <- p + geom_boxplot(aes(fill=Label))
p <- p + geom_jitter()
p <- p + facet_wrap( ~ variable, scales="free")
p <- p + xlab("x-axis") + ylab("y-axis") + ggtitle("Title")
p <- p + guides(fill=guide_legend(title="Legend_Title"))
p 

编辑 3: 如何将 geom_point() 点与箱线图的中心对齐?可以使用 position_dodge 来完成.这应该有效.

Edit 3: How to align geom_point() points to the center of box-plot? It could be done using position_dodge. This should work.

require(ggplot2)
p <- ggplot(data = df.m, aes(x=variable, y=value)) 
p <- p + geom_boxplot(aes(fill = Label))
# if you want color for points replace group with colour=Label
p <- p + geom_point(aes(y=value, group=Label), position = position_dodge(width=0.75))
p <- p + facet_wrap( ~ variable, scales="free")
p <- p + xlab("x-axis") + ylab("y-axis") + ggtitle("Title")
p <- p + guides(fill=guide_legend(title="Legend_Title"))
p 

这篇关于在一张图中绘制多个箱线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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