Poo*_*yan 1 r spacing ggplot2 boxplot
我有一个数据集,我用它制作了一个箱线图。但我想在两组数据之间添加一些空间。
这是我的数据:
structure(list(variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L), levels = c("dm4m_c", "dm4l_c", "dm3_c",
"dm2_c", "dm1_c", "dldm_c", "dm4m_i", "dm4l_i", "dm3_i", "dm2_i",
"dm1_i"), class = "factor"), value = c(4.54, 3.73, 3.06, 2.92,
3.01, 4.45, 2.36, 3.35, 4.35, 5.67, 2.91, 3.72, 5.39, 3.4, 3.07,
1.2, 0.79, 0.43, 0.17, 0.03, 0.6, 0.15, -0.24, 0.98, 0.15, -0.37,
0.2, 1.71, 0.46, 0.26, 0.46, 0.32, 0.49, -0.03, 0.39, 0.6, 0.47,
0.32, 1.17, 0.55, -0.48, 0.13, 1.65, -0.2, 0.05, 2.86, 2.19,
2.06, 1.66, 2, 2.95, 0.45, 1.29, 1.96, 1.07, 0.52, 0.27, 3.17,
1.24, 1.03, -0.24, -0.41, -0.45, NA, NA, NA, -0.36, -0.39, -0.13,
-0.49, -0.81, -0.58, 0.69, -0.93, -0.43, 0.28, -0.14, -0.04,
-0.31, 0.27, 0.34, 0.9, 0.28, 0.8, 0.25, -0.37, -0.35, 0.59,
-0.29, -0.07, 3.8, 2.28, 2.43, 1.29, 1.01, 2.65, 1.75, 2.42,
3.86, 4.74, 1.7, 1.74, 2.55, 1.58, 1.32, 0.88, 0.31, 0.2, -0.9,
-1.14, 0.4, 0.06, 0.06, 0.69, 0.81, 0.24, 0.17, 0.73, -0.1, 0.35,
0.28, 0.03, -0.01, 0.13, 0.42, 0.79, 0.18, -0.02, 0.43, 1.68,
-0.08, 0.23, 0.98, -0.6, -0.23, 2.02, 1.24, 1.46, 0.8, 0.89,
1.89, 0.19, 0.55, 1.34, 1.05, 0, 0.04, 1.81, 0.2, 0.21, -0.09,
-0.41, -0.4, NA, NA, NA, -0.54, -0.77, -0.32, -0.04, -0.98, -0.29,
0.8, -0.99, -0.68), group = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), levels = c("1",
"2"), class = "factor")), row.names = c(NA, -165L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)
这是我制作的箱线图,我想制作一个空格而不是虚线:

我尝试过不同的事情。我把它们组成一组并使用过facet_wrap,但结果不是我想要的:

这是我的最终代码:
ggplot(data_long2, aes(x = variable, y = value,fill=variable)) + # Applying ggplot function
geom_boxplot(position=position_dodge(width=5)) +
geom_point() +
stat_summary(fun.y="mean", color="white", shape=7)+
theme_classic()+
ggtitle("Slop") +
xlab("Zones") + ylab("Slop") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5),legend.position = "none") +
geom_vline(xintercept = 6.5, linetype = "dashed")
Run Code Online (Sandbox Code Playgroud)
使用的问题facet_wrap很可能是您使用了scales="free"这将释放 x 和 y 比例,并且最终得到两个面的 y 轴。相反,你可以使用"free_x". 但是,facet_wrap我建议不要使用它,而是切换到facet_grid提供使用选项的space="free_x". 这样做时,面的大小将根据类别数量进行调整,并确保两个面板中箱线图的宽度相同。此外,我还添加了一些theme调整来消除刻面外观,并通过增加expansionx 比例来添加更多空间:
library(ggplot2)
ggplot(data_long2, aes(x = variable, y = value, fill = variable)) +
geom_boxplot(position = position_dodge(width = 5)) +
geom_point() +
stat_summary(fun.y = "mean", color = "white", shape = 7) +
scale_x_discrete(expand = expansion(add = 1.2)) +
facet_grid(.~group, scales = "free_x", space = "free_x") +
theme_classic() +
ggtitle("Slop") +
xlab("Zones") +
ylab("Slop") +
theme(
axis.text.x = element_text(angle = 90, vjust = 0.5),
legend.position = "none",
strip.text.x = element_blank(),
strip.background.x = element_blank(),
panel.spacing.x = unit(0, "pt")
)
Run Code Online (Sandbox Code Playgroud)
我最近添加了一个名为scale_x_manual(){ggh4x} 的函数,它允许您手动将离散值放置在连续位置。您可以使用它将第二组放置得稍远一些。不幸的是,它目前有一个错误,仅接受“双”向量(而不是整数)。下面的例子(与问题无关的部分被省略)。
library(ggplot2)
library(ggh4x)
ggplot(data_long2, aes(x = variable, y = value, fill = variable)) +
geom_boxplot() +
scale_x_manual(as.double(c(1:6, 8:13))) # set numbers to your liking
#> Warning: Removed 6 rows containing non-finite values (`stat_boxplot()`).
Run Code Online (Sandbox Code Playgroud)

免责声明:我是 ggh4x 的作者。