在一个图中绘制多个箱图

在一个图中绘制多个箱图

我将数据保存为.csv12列的文件。第2至11列(标记为F1, F2, ..., F11featuresColumn one包含或label这些功能。goodbad

我想绘制boxplot所有这些功能11label,而是通过单独goodbad。到目前为止我的代码是:

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

然而,这只能说明F1反对label

我的问题是:如何显示F2, F3, ..., F11label在一个图表一些dodge position?我已将这些特征标准化,因此它们在[0 1]范围内具有相同的比例。

测试数据可以在这里找到。我手工绘制了一些东西来解释这个问题(见下文)。


守着星空守着你
浏览 813回答 3
3回答

jeck猫

在绘制之前,您应该通过熔化数据(参见下面的熔化数据的样子)来获取特定格式的数据。否则,你所做的似乎没问题。require(reshape2)df <- read.csv("TestData.csv", header=T)# melting by "Label". `melt is from the reshape2 package.&nbsp;# 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#&nbsp; &nbsp; &nbsp;Label variable&nbsp; &nbsp; &nbsp; value# 1&nbsp; &nbsp; Good&nbsp; &nbsp; &nbsp; &nbsp;F1 0.64778924# 2&nbsp; &nbsp; Good&nbsp; &nbsp; &nbsp; &nbsp;F1 0.54608791# 3&nbsp; &nbsp; Good&nbsp; &nbsp; &nbsp; &nbsp;F1 0.46134200# 4&nbsp; &nbsp; Good&nbsp; &nbsp; &nbsp; &nbsp;F1 0.79421221# 5&nbsp; &nbsp; Good&nbsp; &nbsp; &nbsp; &nbsp;F1 0.56919951# 6&nbsp; &nbsp; Good&nbsp; &nbsp; &nbsp; &nbsp;F1 0.73568570# 7&nbsp; &nbsp; Good&nbsp; &nbsp; &nbsp; &nbsp;F1 0.65094207# 8&nbsp; &nbsp; Good&nbsp; &nbsp; &nbsp; &nbsp;F1 0.45749702# 9&nbsp; &nbsp; Good&nbsp; &nbsp; &nbsp; &nbsp;F1 0.80861929# 10&nbsp; &nbsp;Good&nbsp; &nbsp; &nbsp; &nbsp;F1 0.67310067# 11&nbsp; &nbsp;Good&nbsp; &nbsp; &nbsp; &nbsp;F1 0.68781739# 12&nbsp; &nbsp;Good&nbsp; &nbsp; &nbsp; &nbsp;F1 0.47009455# 13&nbsp; &nbsp;Good&nbsp; &nbsp; &nbsp; &nbsp;F1 0.95859182# 14&nbsp; &nbsp;Good&nbsp; &nbsp; &nbsp; &nbsp;F1 1.00000000# 15&nbsp; &nbsp;Good&nbsp; &nbsp; &nbsp; &nbsp;F1 0.46908343# 16&nbsp; &nbsp; Bad&nbsp; &nbsp; &nbsp; &nbsp;F1 0.57875528# 17&nbsp; &nbsp; Bad&nbsp; &nbsp; &nbsp; &nbsp;F1 0.28938046# 18&nbsp; &nbsp; Bad&nbsp; &nbsp; &nbsp; &nbsp;F1 0.68511766require(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)) +&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;geom_boxplot(aes(fill=Label))p + facet_wrap( ~ variable, scales="free")ggplot2_faceted编辑2:如何添加x-labels,y-labels,title,改变legend heading,添加jitter?p <- ggplot(data = df.m, aes(x=variable, y=value))&nbsp;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&nbsp;ggplot2_geom_plot编辑3:如何将geom_point()点对齐到箱形图的中心?它可以使用position_dodge。这应该工作。require(ggplot2)p <- ggplot(data = df.m, aes(x=variable, y=value))&nbsp;p <- p + geom_boxplot(aes(fill = Label))# if you want color for points replace group with colour=Labelp <- 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&nbsp;

回首忆惘然

既然你没有提到一个情节包,我在这里建议使用Lattice版本(我认为ggplot2答案比格子更多,至少因为我在这里)。&nbsp;##&nbsp;reshaping&nbsp;the&nbsp;data(&nbsp;similar&nbsp;to&nbsp;the&nbsp;other&nbsp;answer) &nbsp;library(reshape2) &nbsp;dat.m&nbsp;<-&nbsp;melt(TestData,id.vars='Label') &nbsp;library(lattice) &nbsp;bwplot(value~Label&nbsp;|variable,&nbsp;&nbsp;&nbsp;&nbsp;##&nbsp;see&nbsp;the&nbsp;powerful&nbsp;conditional&nbsp;formula&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data=dat.m, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;between=list(y=1), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;main="Bad&nbsp;or&nbsp;Good")
打开App,查看更多内容
随时随地看视频慕课网APP