ggplot:使用 stat_summary 时如何更改箱线图设置
ggplot: How to change boxplot settings when stat_summary is used
我想要分组箱线图,其中胡须由 stat_summary 定义。在 changing-whisker-definition 的帮助下,我编写了以下代码:
# 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()
其中给出了以下图表:
我想进行一些更改:
- 如何更改方框的宽度?
- 如何用相同颜色的边框填充方框?
我很乐意提供任何帮助。非常感谢。
将 fill
美学映射到 grp
并为其添加类似的比例。我使用略有不同的颜色使均值可见。
要更改箱线图宽度,请使用 ggsave
和各种 width
参数,箱线图将自动调整。如果你想在两者之间添加一些 space,你将不得不作弊,见下文。
结合stat_summary
修改宽度不容易:虽然geom_bar
和geom_boxplot
有一个width
参数,但是我做不到与 stat_summary
一起正常工作。相反,我在 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)
在 position_dodge
中围绕 width
进行进一步调整。
我想要分组箱线图,其中胡须由 stat_summary 定义。在 changing-whisker-definition 的帮助下,我编写了以下代码:
# 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()
其中给出了以下图表:
我想进行一些更改:
- 如何更改方框的宽度?
- 如何用相同颜色的边框填充方框?
我很乐意提供任何帮助。非常感谢。
将 fill
美学映射到 grp
并为其添加类似的比例。我使用略有不同的颜色使均值可见。
要更改箱线图宽度,请使用 ggsave
和各种 width
参数,箱线图将自动调整。如果你想在两者之间添加一些 space,你将不得不作弊,见下文。
结合stat_summary
修改宽度不容易:虽然geom_bar
和geom_boxplot
有一个width
参数,但是我做不到与 stat_summary
一起正常工作。相反,我在 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)
在 position_dodge
中围绕 width
进行进一步调整。