如果按组缺少数据,则框线图的宽度是否一致? [英] Consistent width of boxplots if missing data by group?

查看:121
本文介绍了如果按组缺少数据,则框线图的宽度是否一致?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前曾针对 barplots 讨论过类似的问题,但缺少针对 boxplots 的解决方案:



删除1组的值:

  #将数据的子集分组为组:
data.sub< -subset(data,treatment!= high |品种!= E)

wi ndows(4,3)
ggplot(data.sub,aes(x = variety,y = note,fill = treatment))+
geom_boxplot()

缺少数据的箱形图比另一个箱形图宽:








有没有办法保持恒定的宽度

解决方案

我们可以利用保留 position_dodge 中的参数。



来自?position_dodge


preserve:应该躲避保留某个位置的所有元素的总宽度还是单个元素的宽度?




  ggplot(data.sub,aes(x = variety,y = note,fill = treatment))+ 
geom_boxplot(position = position_dodge(preserve = single))


I have a similar question previously discussed for barplots, but with missing solution for boxplots: Consistent width for geom_bar in the event of missing data

I would like to produce a boxplots by groups. However, data for some groups can be missing, leading to increased width of boxplots with missing groups.

I tried to specify geom_boxplot(width = value) or geom_boxplot(varwidth = F), but this does not work.

Also, as suggested for barplots example, I tried to add NA values for missing data group. Boxplot just only skipp missing data, and extent the boxplot width. I got back the warning:

Warning messages:
1: Removed 1 rows containing non-finite values (stat_boxplot). 


Dummy example:

# library
library(ggplot2)

# create a data frame
variety=rep(LETTERS[1:7], each=40)
treatment=rep(c("high","low"),each=20)
note=seq(1:280)+sample(1:150, 280, replace=T)

# put data together
data=data.frame(variety, treatment ,  note)

ggplot(data, aes(x=variety, y=note, fill=treatment)) + 
  geom_boxplot()

Boxplots have the same width if there are values for each group:

Remove the values for 1 group:

# subset the data to have a missing data for group:
data.sub<-subset(data, treatment != "high" | variety != "E" )

windows(4,3)
ggplot(data.sub, aes(x=variety, y=note, fill=treatment)) + 
  geom_boxplot()

Boxplot with missing data is wider than another ones:


Is there a way how to keep constant width of boxplots?

解决方案

We can make use of the preserve argument in position_dodge.

From ?position_dodge

preserve: Should dodging preserve the total width of all elements at a position, or the width of a single element?

ggplot(data.sub, aes(x=variety, y=note, fill=treatment)) + 
 geom_boxplot(position = position_dodge(preserve = "single"))

这篇关于如果按组缺少数据,则框线图的宽度是否一致?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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