我之前曾针对 barplots 讨论过类似的问题,但缺少针对 boxplots 的解决方案:Consistent width for geom_bar in the event of missing data
我想按组生成箱形图。但是,某些组的数据可能会丢失,从而导致缺少组的箱形图的宽度增加。
我尝试指定geom_boxplot(width = value
)或geom_boxplot(varwidth = F)
,但这不起作用。
此外,正如Barplots示例所建议的那样,我尝试为丢失的数据组添加NA
值。 Boxplot仅跳过缺少的数据,并扩展boxplot的宽度。我得到了警告:
Warning messages:
1: Removed 1 rows containing non-finite values (stat_boxplot).
虚拟示例:
# 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()
如果每个组都有值,则方框图的宽度相同:
删除1组的值:
# 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()
缺少数据的箱形图比另一个箱形图宽:
有没有办法保持箱形图的宽度恒定?