在ggplot2中删除geom_boxplot中的边框 [英] Removing the borders in geom_boxplot in ggplot2

查看:1321
本文介绍了在ggplot2中删除geom_boxplot中的边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该看起来相对简单,但我找不到让我做这件事的争论,并且我已经搜索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)

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



我试过包括 color = cond 但是可以去掉中位数。

解决方案

可以使用与Marco Sandri的答案相同的数据集。



Hack 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)



这个覆盖原始boxplot的版本基本上是外框的轮廓,隐藏了中间值( fatten = NULL ),填充颜色( fill = NA ),胡须( coef = 0 )&异常值( outlier.alpha = 0 )。



然而,似乎并不能很好地处理。我用ggplot2的dev版本测试了它(积极推荐),但无济于事。见下面的输出:





Hack 2 即可。如果您需要它积极工作:

  ggplot(dat%>%
group_by(cond)%> ;
mutate(rating.IQR = case_when(等级<=分位数(等级,0.3)〜分位数(等级,0.25),
TRUE〜分位数(等级,0.75))),
aes(x = cond,y = rating,fill = cond))+
geom_boxplot()+
geom_boxplot(aes(color = cond,y = rating.IQR),
fatten = NULL,fill = NA)

(ggplot输出与上面相同)

似乎并不了解 coef = 0 & output.alpha = 0 命令,所以这个hack创建了一个y变量的修改版本,这样P30下面的所有内容都被设置为P25,并且上面的所有内容都被设置为P75。这创造了一个没有异常值的盒子,没有胡须,中位值与P75的上限值一起坐在一起。

这样更麻烦,但它的工作原理是: / p>


This should seem relatively straightforward but I can't find an argument which would allow me to do this and I've searched Google and Stack for an answer.

Sample code:

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)

This outputs the first graph, I would want something like the second.

I tried including colour=cond but that gets rid of the median.

解决方案

Two possible hacks for consideration, using the same dataset as Marco Sandri's answer.

Hack 1. If you don't really need it to work in plotly, just static ggplot image:

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)

This overlays the original boxplot with a version that's essentially an outline of the outer box, hiding the median (fatten = NULL), fill colour (fill = NA), whiskers (coef = 0) & outliers (outlier.alpha = 0).

However, it doesn't appear to work well with plotly. I've tested it with the dev version of ggplot2 (as recommended by plotly) to no avail. See output below:

Hack 2. If you need it to work in plotly:

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 output is same as above)

plotly doesn't seem to understand the coef = 0 & output.alpha = 0 commands, so this hack creates a modified version of the y variable, such that everything below P30 is set to P25, and everything above is set to P75. This creates a boxplot with no outliers, no whiskers, and the median sits together with the upper box limit at P75.

It's more cumbersome, but it works in plotly:

这篇关于在ggplot2中删除geom_boxplot中的边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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