R:使用数据框中的列绘制多个箱形图 [英] R: Plot multiple box plots using columns from data frame

查看:585
本文介绍了R:使用数据框中的列绘制多个箱形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为数据框中每个不相关的列绘制一个独立的箱形图。我以为我在 sfsmsic 包中的 boxplot.matrix 处在正确的轨道上,但它似乎做的相同作为 boxplot(as.matrix(plotdata)),它是在轴上具有共享比例的共享盒图中绘制所有内容。我要(说)5个单独的图。 p>

我可以手动执行以下操作:

  par(mfrow = c (2,2))
boxplot(数据$ var1
boxplot(数据$ var2)
boxplot(数据$ var3)
boxplot(数据$ var4)

但是必须有一种使用数据框列的方法吗?



编辑:我使用了迭代,请看我的答案。

解决方案

您可以使用整形软件包以简化操作

  data<-data.frame(v1 = rnorm(100),v2 = rnorm(100),v3 = rnorm(100),v4 = rnorm(100))
库(重塑)
meltData<-熔化(数据)
boxplot(data = meltData,值〜variable)

甚至使用 ggplot2 软件包,使事情变得更好

  library(ggplot2)
p<-ggplot( meltData,aes(factor(variable),value))
p + geom_boxplot()+ facet_wrap(〜variable,scale = free)


I would like to plot an INDIVIDUAL box plot for each unrelated column in a data frame. I thought I was on the right track with boxplot.matrix from the sfsmsic package, but it seems to do the same as boxplot(as.matrix(plotdata) which is to plot everything in a shared boxplot with a shared scale on the axis. I want (say) 5 individual plots.

I could do this by hand like:

par(mfrow=c(2,2))
boxplot(data$var1
boxplot(data$var2)
boxplot(data$var3)
boxplot(data$var4)

But there must be a way to use the data frame columns?

EDIT: I used iterations, see my answer.

解决方案

You could use the reshape package to simplify things

data <- data.frame(v1=rnorm(100),v2=rnorm(100),v3=rnorm(100), v4=rnorm(100))
library(reshape)
meltData <- melt(data)
boxplot(data=meltData, value~variable)

or even then use ggplot2 package to make things nicer

library(ggplot2)
p <- ggplot(meltData, aes(factor(variable), value)) 
p + geom_boxplot() + facet_wrap(~variable, scale="free")

这篇关于R:使用数据框中的列绘制多个箱形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆