我将数据保存为包含 12 列的 .csv
文件。第 2 列到第 11 列(标记为 F1、F2、...、F11
)是功能
。 第一列
包含这些功能的标签
,可以是好
或坏
。
我想根据标签
绘制所有这11个特征的箱线图
,但用good
分隔> 或坏
。到目前为止我的代码是:
qplot(Label, F1, data=testData, geom = "boxplot", fill=Label,
binwidth=0.5, main="Test") + xlab("Label") + ylab("Features")
但是,这仅针对标签
显示F1
。
我的问题是:如何在一张图表中针对标签
显示F2、F3、...、F11
以及一些闪避位置
?我已经对这些特征进行了标准化,因此它们在 [0 1] 范围内具有相同的比例。
测试数据可以查到here 。我手绘了一些东西来解释这个问题(见下文)。
最佳答案
在绘图之前,您应该通过熔化数据来获取特定格式的数据(有关熔化数据的外观,请参阅下文)。否则,你所做的似乎没问题。
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))
编辑:我意识到您可能需要分面。这也是它的一个实现:
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
、更改图例标题
,添加抖动
?
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来完成。这应该可行。
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
关于r - 在一张图中绘制多个箱线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14604439/