英文:
Add space between groups of boxplots in ggplot2
问题
我有一个数据集并从中制作了箱线图。但我想在我的数据的两组之间添加一些间距。
这是我的数据:
这是我制作的箱线图,我想要在虚线的位置添加一些间距:
我尝试了不同的方法。我将它们分组并使用了 facet_wrap
,但结果不是我想要的:
这是我的最终代码:
英文:
I have a dataset and I have made a boxplot from it. But I want to add some space between the two groups of my data.
This is my data:
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")
This is the boxplot that I made and I want to make a space instead of the dashline:
I have tried different things. I make the group of them and I have use facet_wrap
but the result was not what I intended:
This is my final code:
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")
答案1
得分: 5
使用facet_wrap
的问题很可能是您使用了scales="free"
,这会释放x和y轴刻度,导致两个分面都有y轴。相反,您可以使用"free_x"
。不过,我建议切换到facet_grid
,它提供了使用space="free_x"
选项。这样做可以根据类别数量调整分面的大小,确保两个面板中箱线图的宽度相同。此外,我对theme
进行了一些调整,以消除分面外观,并通过增加x轴刻度的expansion
来增加一些空间:
<details>
<summary>英文:</summary>
The issue with using `facet_wrap` was most likely that you used `scales="free"` which will free both the x and y scale and you end up with a y axis for both facets. Instead you could use `"free_x"`. However, instead of using `facet_wrap` I would suggest to switch to `facet_grid` which offers the option to use `space="free_x"`. Doing so the size of the facets will be adjusted for the number of categories and ensure the same width for the boxplots in both panels. Additionally I added some `theme` adjustments to get rid of the facet look and added some more space by increasing the `expansion` of the x scale:
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")
)
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/yHsm4.png
</details>
# 答案2
**得分**: 4
最近我在{ggh4x}中添加了一个名为`scale_x_manual()`的函数,允许你在连续位置手动放置离散值。你可以使用这个功能将第二组值放置得更远一些。不幸的是,目前存在一个 bug,只有 'double' 向量(而不是整数)被接受。下面是一个示例(与问题无关的部分已省略)。
```r
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))) # 将数字设置为你喜欢的值
#> 警告: 删除了包含非有限值的 6 行 (`stat_boxplot()`).
<!-- -->
免责声明:我是 ggh4x 的作者。
英文:
I've recently added a function called scale_x_manual()
to {ggh4x}, which lets you manually place discrete values at continuous positions. You can use this to place your second set a bit further away. Unfortunately, it currently has a bug where only 'double' vectors (and not integers) are accepted. Example below (parts not related to question omitted).
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()`).
<!-- -->
Disclaimer: I'm the author of ggh4x.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论