小编Mar*_*c C的帖子

来自ggplot2的geom_boxplot():强制显示空白级别

我找不到一种方法来让ggplot2在一个boxplot中显示一个空的级别,而不会将我的数据帧与实际的缺失值相混淆.这是可重现的代码:

# fake data
dftest <- expand.grid(time=1:10,measure=1:50)
dftest$value <- rnorm(dim(dftest)[1],3+0.1*dftest$time,1)

# and let's suppose we didn't observe anything at time 2

# doesn't work even when forcing with factor(..., levels=...)
p <- ggplot(data=dftest[dftest$time!=2,],aes(x=factor(time,levels=1:10),y=value))
p + geom_boxplot()

# only way seems to have at least one actual missing value in the dataframe
dftest2 <- dftest
dftest2[dftest2$time==2,"value"] <- NA
p <- ggplot(data=dftest2,aes(x=factor(time),y=value))
p + geom_boxplot()
Run Code Online (Sandbox Code Playgroud)

所以我想我错过了一些东西.在处理平衡实验时,这不是问题,因为这些缺失数据可能在数据帧中是明确的.但是,例如,在一个队列中观察到的数据,这意味着对未观察到的组合缺少值的数据进行估算...感谢您的帮助.

r ggplot2 boxplot r-factor

10
推荐指数
1
解决办法
6979
查看次数

标签 统计

boxplot ×1

ggplot2 ×1

r ×1

r-factor ×1