绘制2D箱柜图 [英] Drawing 2D boxplots

查看:456
本文介绍了绘制2D箱柜图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用2个参数对数据进行分组:

  set.seed(1)
dat< ; - data.frame(xx = sample(10,9),
yy = sample(20,9),
group = c('A','B','C'))

我可以为每个维度绘制箱形图:

  ggplot(dat,aes(x = group,y = yy,fill = group))+ geom_boxplot()
ggplot(dat,aes(x = group,y = xx ,fill = group))+ geom_boxplot()+ coord_flip()



现在我想将这些结合起来并绘制反映两个变量数据的箱形图,像这样:

(该图像是在图形编辑器中手动制作的)



是否有任何简单的方法可以绘制这种箱型图?

解决方案

不是很好的代表方式。它可能非常混乱。通常数据是这样表示的。

  datPlot<  -  melt(dat)
ggplot(datPlot,aes x = group,y = value,fill = variable))+ geom_boxplot()


或者如果两个变量的范围非常大不同的,你可以尝试faceting

  ggplot(datPlot,aes(x = group,y = value,fill = group))+ geom_boxplot ()
+ facet_wrap(〜variable,scale =free_y)



或这个

  ggplot(dat,aes(x = xx,y = yy,color = group))+ geom_point(size = 3)


I have grouped data with, say, 2 parameters:

set.seed(1)
dat <- data.frame( xx=sample(10,9),
                   yy=sample(20,9),
                   group=c('A','B', 'C')  )

I can draw boxplots for each dimension:

ggplot(dat, aes(x=group, y=yy, fill=group)) + geom_boxplot() 
ggplot(dat, aes(x=group, y=xx, fill=group)) + geom_boxplot() + coord_flip()

Now I would to combine these and draw boxplots reflecting data on both variables, something like this: (this image was made manually in graphic editor)

Is there any easy way to draw this kind of boxplots?

解决方案

That is not good way of representation. It can be very confusing. Usually that data is represented like this.

datPlot <- melt(dat)
ggplot(datPlot, aes(x=group, y=value, fill=variable)) + geom_boxplot() 

or if the range of two variables is very different you can try faceting

ggplot(datPlot, aes(x=group, y=value, fill=group)) + geom_boxplot()
 +facet_wrap(~variable,scale="free_y")

or this

ggplot(dat, aes(x=xx, y=yy, color=group)) + geom_point(size=3) 

这篇关于绘制2D箱柜图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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