ggplot:如何在使用stat_summary时更改Boxplot设置 [英] ggplot: How to change boxplot settings when stat_summary is used

查看:638
本文介绍了ggplot:如何在使用stat_summary时更改Boxplot设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想分组的盒子,其中胡须由stat_summary定义。借助于不断变化的胡须定义,我编写了以下代码:
$ p $ #Data
xdf2< - data.frame(month = rep(1:6,each = 100)
,grp = rep(c('A','B'),50 * 6)

xdf2 $ m < - rpois(n = nrow(xdf2),10)
$晶须的定义
f < - 函数(x){
r < - 分位数(x,probs = c(0.10,0.25,0.5,0.75,0.90))
名称r)< - c(ymin,lower,middle,upper,ymax)
r
}

# (x,probs = 0.1)| quantile(x,probs = 0.9)< x)
} $($)


$ b#地块
ggplot(数据= xdf2
,aes(因子(月),m,颜色= grp)
)+
stat_summary(fun.data = f
,geom =boxplot
,position = position_dodge(width = 1)
,size = 1
)+
stat_summary(fun.y = o,geom =point,position = position_dodge(width = 1))+
scale_color_manual( values = c(gray30,darkgrey),labels = c(AAA,BBB))+
theme_bw()

给出以下图表:



我想进行一些更改:


  • 如何更改框的宽度?

  • 如何使用相同颜色的边框填充框?



我会很乐意提供任何帮助。

解决方案

Map fill 唯美至 grp 并为其添加类似的比例。我使用稍微不同的颜色来使平均值可见。



要更改箱线宽度,请使用 ggsave 宽度参数,箱型图会自动调整。如果您想在两者之间添加一些空间,您必须作弊,请参阅下面的内容。



修改宽度并非易事,与 stat_summary :尽管 geom_bar 和<$有一个宽度参数c $ c> geom_boxplot ,我无法使用 stat_summary 正常工作。相反,我在 scale_x 中使用了一些肮脏的技巧。

  K <  - 长度(独特(xdf2 $ month))
lev < - seq_len(1 + 2 * K)

xdf2 $ month2 < - 因子(2 * xdf2 $ month,
levels = lev)

ggplot(data = xdf2,aes(month2,m,color = grp,fill = grp))+
stat_summary(fun.data = f, geom =boxplot,
position = position_dodge(width = 1.5),size = 1)+
stat_summary(fun.y = o,geom =point,position = position_dodge(width = 1.5) )+
scale_color_manual(values = c(gray30,darkgrey),labels = c(AAA,BBB))+
scale_fill_manual(values = c(gray20, grey75),labels = c(AAA,BBB))+
theme_bw()+
scale_x_discrete(limits = lev,breaks = 1:K * 2,labels = 1:K)


width 在 position_dodge 中播放 code>用于额外的广告justment。


I would like to have grouped boxplots which whiskers is defined by stat_summary. With help of changing-whisker-definition I wrote the following code:

# Data
xdf2 <- data.frame(month = rep(1:6,each=100)
                  , grp = rep(c('A','B'), 50*6)
                  )
xdf2$m <- rpois(n=nrow(xdf2),10)
# Definition of whiskers
f <- function(x) {
  r <- quantile(x, probs = c(0.10, 0.25, 0.5, 0.75, 0.90))
  names(r) <- c("ymin", "lower", "middle", "upper", "ymax")
  r
}

# Add points outside of whiskers
o <- function(x) {
  subset(x, x < quantile(x,probs=0.1) | quantile(x,probs=0.9) < x)
}


# Plot
ggplot(data = xdf2
         , aes(factor(month),m, color=grp)
         ) +
       stat_summary(fun.data = f
                    , geom="boxplot"
                    , position=position_dodge(width=1)
                    , size=1
                    ) +
        stat_summary(fun.y = o, geom="point", position=position_dodge(width=1)) +
        scale_color_manual(values = c("gray30","darkgrey"),labels = c("AAA","BBB")) +
        theme_bw()

which gives the following graphs:

There are some changes I would like to perform:

  • How can I change the width of the boxes?
  • How can I fill the boxes with the same color of the border?

I would be happy for any help. Thanks a lot.

解决方案

Map fill aesthetic to grp and add a similar scale for it. I'm using slightly different colours to make the mean visible.

To change boxplot widths, use ggsave with various width parameters, boxplots will be adjusted automatically. If you would like to add some space in between, you'll have to cheat a bit, see below.

It is not easy to modify width in conjunction with stat_summary: though there is a width parameter for geom_bar and geom_boxplot, I couldn't make it work properly with stat_summary. Instead, I'm using some dirty tricks with scale_x.

K <- length(unique(xdf2$month))
lev <- seq_len(1 + 2 * K)

xdf2$month2 <- factor(2 * xdf2$month, 
                      levels = lev)

ggplot(data = xdf2, aes(month2, m, color = grp, fill = grp)) +
  stat_summary(fun.data = f, geom="boxplot", 
               position=position_dodge(width=1.5), size=1) +
  stat_summary(fun.y = o, geom="point", position=position_dodge(width=1.5)) +
  scale_color_manual(values = c("gray30","darkgrey"),labels = c("AAA","BBB")) +
  scale_fill_manual(values = c("gray20","grey75"),labels = c("AAA","BBB")) +
  theme_bw() +
  scale_x_discrete(limits = lev, breaks = 1:K*2, labels = 1:K) 

Play around width in position_dodge for additional adjustment.

这篇关于ggplot:如何在使用stat_summary时更改Boxplot设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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