ggplot:为每个连续的x组排列多个y变量的箱形图 [英] ggplot: arranging boxplots of multiple y-variables for each group of a continuous x

查看:3226
本文介绍了ggplot:为每个连续的x组排列多个y变量的箱形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为连续x变量的组创建多个变量的箱型图。这些箱子图应该排列在一起,用于每组x.



数据如下所示:

 


$ require(ggplot2)
require(plyr)
library(reshape2)

x< (100)
y.1 - rnorm(100)
y.2 - rnorm(100)
y.3 - rorm(100)$ b $ (x,y.1,y.2,y.3,y.4))b_4 < - rnorm(100)

df < - as.data.frame(cbind

然后我融化了

  dfmelt < -  melt(df,measure.vars = 2:5)

此解决方案中显示的facet_wrap(


这显示了每个旁边的y变量

  ggplot(dfmelt)+ 
geom_boxplot(aes(x = x,y = value,fill = variable))+
facet_grid(〜变量)



现在我想为x的每个bin生成一个这样的图。



什么需要改变或添加?

解决方案

不完全确定你在找什么。这是关闭吗?



  library(ggplot2)
library(plyr)
ggplot(dfmelt,aes(x = factor(round_any (x,0.5)),y = value,fill = variable))+
geom_boxplot()+
facet_grid(。〜variable)+
labs(x =X(binned) )+
theme(axis.text.x = element_text(angle = -90,vjust = 0.4,hjust = 1))

编辑(回复OP的评论)

您可以将Y的每个相邻放入每个垃圾箱通过取出 facet_grid(...)调用,但我不推荐它。

  ggplot(dfmelt,aes(x = factor(round_any(x,0.5)),y = value,fill = variable))+ 
geom_boxplot()+
labs( x =X(binned))+
theme(axis.text.x = element_text(angle = -90,vjust = 0.4,hjust = 1))
<

$ b $ p $ < b

如果你必须这样做t他的方式,使用方面仍然更清楚:

  dfmelt $ bin < -  factor(round_any(dfmelt $ x,0.5)) 
ggplot(dfmelt,aes(x = bin,y = value,fill = variable))+
geom_boxplot()+
facet_grid(。〜bin,scales =free)+
labs(x =X(binned))+
theme(axis.text.x = element_blank())



请注意,在 dfmelt 中添加了 bin 列。这是因为在 facet_grid(...)公式中使用因子(round_any(x,0.5)) doesn' t工作。


I would like to create boxplots of multiple variables for groups of a continuous x-variable. The boxplots should be arranged next to each other for each group of x.

The data looks like this:

require (ggplot2)
require (plyr)
library(reshape2)

set.seed(1234)
x   <- rnorm(100)
y.1 <- rnorm(100)
y.2 <- rnorm(100)
y.3 <- rnorm(100)
y.4 <- rnorm(100)

df <- as.data.frame(cbind(x,y.1,y.2,y.3,y.4))

which I then melted

dfmelt <- melt(df, measure.vars=2:5)    

The facet_wrap as shown in this solution ( Multiple plots by factor in ggplot (facets)) gives me out each variable in an individual plot, but I would like to have the boxplots of each variable next to each other for each bin of x in one diagram.

ggplot(dfmelt, aes(value, x, group = round_any(x, 0.5), fill=variable))+
geom_boxplot() + 
geom_jitter() + 
facet_wrap(~variable)

This shows the y-variables next to each other but does not bin x.

ggplot(dfmelt) +
geom_boxplot(aes(x=x,y=value,fill=variable))+
facet_grid(~variable)

Now I would like to produce such a plot for each bin of x.

What has to be changed or added?

解决方案

Not exactly sure what you're looking for. Is this close?

library(ggplot2)
library(plyr)
ggplot(dfmelt, aes(x=factor(round_any(x,0.5)), y=value,fill=variable))+
  geom_boxplot()+
  facet_grid(.~variable)+
  labs(x="X (binned)")+
  theme(axis.text.x=element_text(angle=-90, vjust=0.4,hjust=1))

EDIT (response to OP's comment)

You can put the Y's next to each other in each bin by just taking out the facet_grid(...) call, but I don't recommend it.

ggplot(dfmelt, aes(x=factor(round_any(x,0.5)), y=value, fill=variable))+
  geom_boxplot()+
  labs(x="X (binned)")+
  theme(axis.text.x=element_text(angle=-90, vjust=0.4,hjust=1))

If you have to do it this way, it's still clearer using facets:

dfmelt$bin <- factor(round_any(dfmelt$x,0.5))
ggplot(dfmelt, aes(x=bin, y=value, fill=variable))+
  geom_boxplot()+
  facet_grid(.~bin, scales="free")+
  labs(x="X (binned)")+
  theme(axis.text.x=element_blank())

Note the addition of a bin column to dfmelt. This is because using factor(round_any(x,0.5)) in the facet_grid(...) formula doesn't work.

这篇关于ggplot:为每个连续的x组排列多个y变量的箱形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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