在一个图表中绘制多个boxplot

我将数据保存为12列的.csv文件。 列2到11(标记为F1, F2, ..., F11 )是featuresColumn one包含这些function的bad label

我想绘制所有这11个特征boxplot ,但分别是good还是bad 。 我的代码到目前为止是:

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

但是,这只能显示F1label

我的问题是:如何显示F2, F3, ..., F11对一个图中的label与一些dodge position ? 我已经对这些特征进行了归一化处理,因此它们在[0 1]范围内处于相同的范围内。

testing数据可以在这里find。 我画了一些东西来解释这个问题(见下文)。

手绘boxplot示例

在绘图之前,您应该通过融合数据来获取特定格式的数据(请参阅下面有关融化数据的样子)。 否则,你所做的似乎没有问题。

 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)) 

boxplot_ggplot2

编辑:我意识到你可能需要面。 这也是一个实现:

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

ggplot2_faceted

编辑2:如何添加x-labelsy-labelstitle ,更改legend heading ,添加一个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 

ggplot2_geom_plot

编辑3:如何将geom_point()点alignment到box-plot的中心? 这可以使用position_dodge完成。 这应该工作。

 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 

ggplot2_position_dodge_geom_point

既然你没有提到一个情节包,我在这里build议使用格式版本(我认为有更多的ggplot2答案比格子,至less因为我在这里)。

  ## reshaping the data( similar to the other answer) library(reshape2) dat.m <- melt(TestData,id.vars='Label') library(lattice) bwplot(value~Label |variable, ## see the powerful conditional formula data=dat.m, between=list(y=1), main="Bad or Good") 

在这里输入图像描述

格子图的ggplot版本:

 library(reshape2) library(ggplot2) df <- read.csv("TestData.csv", header=T) df.m <- melt(df, id.var = "Label") ggplot(data = df.m, aes(x=Label, y=value)) + geom_boxplot() + facet_wrap(~variable,ncol = 4) 

情节: 在这里输入图像描述

使用基础graphics,我们可以使用at =来控制框的位置, boxwex =和框的宽度一起使用。 第一个boxplot语句创build一个空白图。 然后在以下两个语句中添加2个跟踪。

请注意,在下面,我们使用df[,-1]从要排列的值中排除第一个(id)列。 对于不同的数据框,可能需要将其更改为包含要绘制的数据的列的子集。

 df <- data.frame(id = c(rep("Good",200), rep("Bad", 200)), F1 = c(rnorm(200,10,2), rnorm(200,8,1)), F2 = c(rnorm(200,7,1), rnorm(200,6,1)), F3 = c(rnorm(200,6,2), rnorm(200,9,3)), F4 = c(rnorm(200,12,3), rnorm(200,8,2))) boxplot(df[,-1], xlim = c(0.5, ncol(df[,-1])+0.5), boxfill=rgb(1, 1, 1, alpha=1), border=rgb(1, 1, 1, alpha=1)) #invisible boxes boxplot(df[which(df$id=="Good"), -1], xaxt = "n", add = TRUE, boxfill="red", boxwex=0.25, at = 1:ncol(df[,-1]) - 0.15) #shift these left by -0.15 boxplot(df[which(df$id=="Bad"), -1], xaxt = "n", add = TRUE, boxfill="blue", boxwex=0.25, at = 1:ncol(df[,-1]) + 0.15) #shift these right by +0.15 

在这里输入图像描述

我知道这是一个较老的问题,但它也是我的一个问题,虽然接受的答案工作,但有一种方法可以做类似的事情, 而无需使用像ggplot或lattice这样的附加软件包。 这不是很好,箱形图重叠,而不是并排显示,但:

 boxplot(data1[,1:4]) boxplot(data2[,1:4],add=TRUE,border="red") 

这是什么图片。

这样放入两套箱柜,第二箱有红色轮廓(没有填充物),并且把exception点置于红色。 好的是,它适用于两个不同的数据框,而不是试图重塑它们。 快速和肮脏的方式。