【问题标题】:Removing the borders in geom_boxplot in ggplot2删除 ggplot2 中 geom_boxplot 中的边框
【发布时间】:2017-09-02 15:07:11
【问题描述】:

这看起来应该相对简单,但我找不到可以让我这样做的论据,我已经在 Google 和 Stack 中搜索了答案。

示例代码:

library(ggplot2)
library(plotly)

dat <- data.frame(cond = factor(rep(c("A","B"), each=200)), rating = c(rnorm(200),rnorm(200, mean=.8)))

p <- ggplot(dat, aes(x=cond, y=rating, fill=cond)) + geom_boxplot()

p <- ggplotly(p)

这会输出第一张图,我想要第二张图。

我尝试包含colour=cond,但这摆脱了中位数。

【问题讨论】:

    标签: r ggplot2 plotly


    【解决方案1】:

    使用与 Marco Sandri 的答案相同的数据集,两种可能的黑客攻击可供考虑。

    黑客 1。如果你真的不需要它来绘图,只需静态 ggplot 图像:

    ggplot(dat, aes(x=cond, y=rating, fill=cond)) + 
      geom_boxplot() +
      geom_boxplot(aes(color = cond),
                   fatten = NULL, fill = NA, coef = 0, outlier.alpha = 0,
                   show.legend = F)
    

    这覆盖了原始箱线图,其版本基本上是外箱的轮廓,隐藏了中位数 (fatten = NULL)、填充颜色 (fill = NA)、胡须 (coef = 0) 和异常值 (outlier.alpha = 0) .

    但是,它似乎不适用于 plotly。我已经用 ggplot2 的开发版本(由 plotly 推荐)对其进行了测试,但无济于事。请参阅下面的输出:

    黑客 2。如果您需要它在情节中工作:

    ggplot(dat %>%
             group_by(cond) %>%
             mutate(rating.IQR = case_when(rating <= quantile(rating, 0.3) ~ quantile(rating, 0.25),
                                           TRUE ~ quantile(rating, 0.75))), 
           aes(x=cond, y=rating, fill=cond)) + 
      geom_boxplot() +
      geom_boxplot(aes(color = cond, y = rating.IQR),
                   fatten = NULL, fill = NA)
    

    (ggplot输出同上)

    plotly 似乎无法理解 coef = 0output.alpha = 0 命令,因此这个 hack 创建了 y 变量的修改版本,使得 P30 以下的所有内容都设置为 P25,而上面的所有内容都设置为 P75。这将创建一个没有异常值、没有胡须的箱线图,并且中位数与 P75 的箱形上限位于一起。

    虽然比较麻烦,但是在情节上可以用:

    【讨论】:

    • outlier.shape=NA 可能比将 alpha 设置为零更可取,因为它根本不会绘制异常值。 IDK 如果 ggplot(或渲染算法)足够聪明,可以意识到具有alpha=0 的对象实际上不存在。 (可能只有在有数百个异常值时才会产生影响,尤其是在渲染为矢量图形时。)
    【解决方案2】:

    这是一个基于 grobs 的不优雅的解决方案:

    set.seed(1)
    dat <- data.frame(cond = factor(rep(c("A","B"), each=200)), 
                      rating = c(rnorm(200),rnorm(200, mean=.8)))
    
    library(ggplot2)
    library(plotly)
    p <- ggplot(dat, aes(x=cond, y=rating, fill=cond)) + geom_boxplot() 
    
    # Generate a ggplot2 plot grob
    g <- ggplotGrob(p)
    
    # The first box-and-whiskers grob
    box_whisk1 <- g$grobs[[6]]$children[[3]]$children[[1]]
    pos.box1 <- which(grepl("geom_crossbar",names(box_whisk1$children)))
    g$grobs[[6]]$children[[3]]$children[[1]]$children[[pos.box1]]$children[[1]]$gp$col <-
      g$grobs[[6]]$children[[3]]$children[[1]]$children[[pos.box1]]$children[[1]]$gp$fill
    
    # The second box-and-whiskers grob    
    box_whisk2 <- g$grobs[[6]]$children[[3]]$children[[2]]
    pos.box2 <- which(grepl("geom_crossbar",names(box_whisk2$children)))
    g$grobs[[6]]$children[[3]]$children[[2]]$children[[pos.box2]]$children[[1]]$gp$col <-
      g$grobs[[6]]$children[[3]]$children[[2]]$children[[pos.box2]]$children[[1]]$gp$fill
    
    library(grid)
    grid.draw(g)
    

    P.S.据我所知,以上代码不能用于生成plotly 图表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多