R语言 在ggplot2中的箱线图组之间添加间距

o75abkj4  于 2023-04-27  发布在  其他
关注(0)|答案(2)|浏览(457)

我有一个数据集,我已经从它做了一个箱线图。但是我想在两组数据之间添加一些空间。
这是我的数据:

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

这是我做的箱线图,我想用一个空格来代替虚线:

我尝试了不同的东西。我做了一组他们,我用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")
j8ag8udp

j8ag8udp1#

使用facet_wrap的问题很可能是您使用了scales="free",这将释放x和y尺度,并且您最终会为两个方面提供y轴。相反,您可以使用"free_x"。然而,而不是使用facet_wrap,我建议切换到facet_grid,它提供了使用space="free_x"的选项。这样做的话,面的大小将根据类别的数量进行调整,并确保两个面板中的箱线图具有相同的宽度。此外,我添加了一些theme调整来消除面的外观,并通过增加x刻度的expansion来增加更多的空间:

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

wlp8pajw

wlp8pajw2#

我最近在{ggh 4x}中添加了一个名为scale_x_manual()的函数,它可以让你手动将离散值放置在连续位置。你可以使用这个函数将你的第二个集合放置得更远一些。不幸的是,它目前有一个bug,它只接受“双”向量(而不是整数)。下面的例子(省略了与问题无关的部分)。

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()`).

免责声明:我是ggh 4x的作者。

相关问题