有时,在没有胡须的情况下显示一个盒子情节是合适的。在geom_boxplot (ggplot2)中,我们可以通过coef=0来实现这一点。是否有一种方法可以在ggboxplot (gpubrv0.5.0,编写时的当前版本)中实现这一点?我注意到ggboxplot与geom_boxplot有很多共同之处,比如在每种情况下使用outlier.shape=NA来抑制异常值的能力。似乎应该有一个简单的方法来抑制胡须。
发布于 2022-11-23 06:08:32
我无法找到一种在ggboxplot中直接实现的方法,这有点奇怪,因为它将省略号传递给一个geom_boxplot调用,所以我不知道为什么coef=0不到达那里并压制胡子。作为一种权宜之计,您可以修改ggboxplot创建的ggplot对象,并以这种方式移除晶须。以下功能显示了这一点:
ggboxplot_whisker_opt <- function(...)
{
opts <- list(...)
# check if user specified a whiskers argument and set options accordingly
if("whisker" %in% names(opts))
{
whisk <- opts$whisker
opts$whisker <- NULL
} else {
whisk <- TRUE
}
pl <- do.call(ggboxplot,opts) # create plot by calling ggboxplot with all user options
if(!whisk)
{
pl_list <- ggplot_build(pl) # get listed version of ggplot object to modify
pl_list$data[[1]]$ymin <- NA # remove the ymin/max that specify the whiskers
pl_list$data[[1]]$ymax <- NA
pl <- ggplot_gtable(pl_list) # convert back to ggplot object
}
# plot the ggplot and return
plot(pl)
}现在,我们可以用whisker=TRUE/FALSE调用这个函数,也可以不使用它,它生成了相应的情节:
set.seed(123)
x <- rnorm(100)
labels <- round(runif(100,1,2))
df <- data.frame(labels=labels,
value=x)
ggboxplot_whisker_opt(df,"labels","value")
# is the same as
ggboxplot_whisker_opt(df,"labels","value",whisker=TRUE)

ggboxplot_whisker_opt(df,"labels","value",whisker=FALSE)

https://stackoverflow.com/questions/74541555
复制相似问题