为了账号安全,请及时绑定邮箱和手机立即绑定

在一个图中绘制多个箱图

在一个图中绘制多个箱图

守着星空守着你 2019-08-26 14:55:23
在一个图中绘制多个箱图我将数据保存为.csv12列的文件。第2至11列(标记为F1, F2, ..., F11)features。Column one包含或label这些功能。goodbad我想绘制boxplot的所有这些功能11对label,而是通过单独good或bad。到目前为止我的代码是:qplot(Label, F1, data=testData, geom = "boxplot", fill=Label,            binwidth=0.5, main="Test") + xlab("Label") + ylab("Features")然而,这只能说明F1反对label。我的问题是:如何显示F2, F3, ..., F11对label在一个图表一些dodge position?我已将这些特征标准化,因此它们在[0 1]范围内具有相同的比例。测试数据可以在这里找到。我手工绘制了一些东西来解释这个问题(见下文)。
查看完整描述

3 回答

?
jeck猫

TA贡献1909条经验 获得超7个赞

在绘制之前,您应该通过熔化数据(参见下面的熔化数据的样子)来获取特定格式的数据。否则,你所做的似乎没问题。


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-labels,y-labels,title,改变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"))

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


查看完整回答
反对 回复 2019-08-26
?
回首忆惘然

TA贡献1847条经验 获得超11个赞

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

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


查看完整回答
反对 回复 2019-08-26
  • 3 回答
  • 0 关注
  • 806 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信