在一张图中绘制多个箱形图 [英] Plot multiple boxplot in one graph
问题描述
我将数据保存为包含12列的 .csv
文件。第2至11列(标记为 F1,F2,...,F11
)为特征
。 第一列
包含这些要素 good
的标签
或坏
。
我想将所有这11个功能的 boxplot
与 label
,但由 good
或 bad
分隔。我的代码到目前为止:
pre $ qplot(Label,F1,data = testData,geom =boxplot,fill = Label,
binwidth = 0.5,main =Test)+ xlab(Label)+ ylab(Features)
然而,这只显示 F1
对标签
。
我的问题是:如何针对标签显示
在一个图中有一些 F2,F3,...,F11
闪避位置
?我已经规范化了这些功能,因此它们的范围在[0 1]范围内。
可以找到测试数据 此处 。我已经手工绘制了一些东西来解释这个问题(见下文)。
您应该通过融合数据来获取特定格式的数据在绘制之前,请参阅下面的数据看起来是如何融化的)。
require(reshape2)
df< - read.csv (TestData.csv,header = T)
#按标签融化。 `融化来自reshape2包装。
#do?融化看看它可以做什么其他事情(你肯定会需要它)
df.m< - melt(df,id.var =Label)
> ; df.m#粘贴一些熔化的数据行。
#标签变量值
#1良好F1 0.64778924
#2良好F1 0.54608791
#3好F1 0.46134200
#4好F1 0.79421221
#5好F1 0.56919951
#6好F1 0.73568570
#7好F1 0.65094207
#8好F1 0.45749702
#9好F1 0.80861929
#10好F1 0.67310067
#11好F1 0.68781739
#12好F1 0.47009455
#13好F1 0.95859182
#14好F1 1.00000000
#15良好F1 0.46908343
#16不良F1 0.57875528
#17不良F1 0.28938046
#18不良F1 0.68511766
需要(ggplot2)
ggplot(data = df.m,aes(x = variable,y = value))+ geom_boxplot(aes(fill = Label))
<强>编辑:我意识到你可能需要面。这里也有一个实现:
p < - ggplot(data = df.m,aes(x = variable, y = value))+
geom_boxplot(aes(fill = Label))
p + facet_wrap(〜variable,scales =free)
编辑2:如何添加 x-labels
, y-labels
, title ,更改
图例标题
,添加 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)$ (fill)= guide_legend(title =b-1)b $ bp <-p + xlab(x-轴)+ ylab(y-轴)+ ggtitle Legend_Title))
p
编辑3:如何将 geom_point()
指向box-plot的中心?可以使用 position_dodge
来完成。这应该有效。
require(ggplot2)
p < - ggplot(data = df.m,aes(x =变量,y =值))
p < - p + geom_boxplot(aes(fill = Label))
#如果你想要点的颜色替换group with color = 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轴)+ ylab(y轴)+ ggtitle(标题)
p < - p + guides(fill = guide_legend(title =Legend_Title))
p
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
.
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")
However, this only shows F1
against the label
.
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))
Edit: 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")
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
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屋!