r - 在一张图中绘制多个箱线图

标签 r plot ggplot2 boxplot

我将数据保存为包含 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 。我手绘了一些东西来解释这个问题(见下文)。

hand-drawn boxplot example

最佳答案

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

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、更改图例标题,添加抖动

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()点与箱线图的中心对齐?可以使用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

关于r - 在一张图中绘制多个箱线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14604439/

相关文章:

r - 子图中共享图例

r - 调度 R 脚本

python - 哪种是绘制 pandas 数据框最适合 future 的方法?

r - ggplot : position legend in top left

r - 使用 ggplot2 手动着色置信区间

python - 在 python 3 中使用 matplotlib scatter 函数时如何更改点子集的绘图标记

r - 在ggplot2中定位水平箱线图

r - 如何告诉 R 去哪里找包裹?

linux - 使用 tm : bug on linux? 在 R 函数中分配全局变量

python - 我使用 sympy 绘制了一个隐式函数。然而,情节看起来并不像曲线