繁体   English   中英

增加 R 中箱线图组之间的空间

[英]Increasing space between boxplot-groups in R

我认为这可能很容易解决,但是,我无法改变分组箱线图中两组之间的距离。

v1 <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2)
v2 <- c(0,0,0,0,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0,0, 1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2)
v3 <- c(13,67,89,280,40,1,23,99,32,1,75,280,270,200,196,300,320, 277,23,4,1,2,5,89,45,23,11,1,3,23,43,90,1,7,250,200,100,11,6,6,123,12, 86,11,300,299,320,190,170,150,344,244,123,234,45)

summary <- data.frame(v1, v2, v3)

summary$v1 <- as.factor(summary$v1)
summary$v2 <- as.factor(summary$v2)

ggplot(summary, aes(x = v1, y = v3, fill = v2)) + geom_boxplot(width = 1, size = 0.4) + geom_dotplot(aes(fill = v2), binaxis='y',  stackdir='center', binwidth=5, position = position_dodge(1))

我创建了一个带有箭头的图形,显示了我想在组之间添加更多空间的位置。 在此先感谢您的帮助。

箱线图示例

您需要将width更改为<1以允许dodge定位完成其工作(并为箱线图几何添加一些闪避定位)。

例如

summary |>
  ggplot(aes(x    = v1,
             y    = v3,
             fill = v2)) +
  geom_boxplot(width    = 0.5,
               position = position_dodge(0.6)) +
  geom_dotplot(binaxis  = "y", 
               stackdir = "center",
               binwidth = 5,
               position = position_dodge(0.6))

在此处输入图像描述

您可以将facet_wrapscales = "free_x"一起使用:

(如果愿意,添加theme将删除条形标题。)

library(tidyverse)

v1 <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
v2 <- c(0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
v3 <- c(13, 67, 89, 280, 40, 1, 23, 99, 32, 1, 75, 280, 270, 200, 196, 300, 320, 277, 23, 4, 1, 2, 5, 89, 45, 23, 11, 1, 3, 23, 43, 90, 1, 7, 250, 200, 100, 11, 6, 6, 123, 12, 86, 11, 300, 299, 320, 190, 170, 150, 344, 244, 123, 234, 45)

summary <- data.frame(v1, v2, v3)

summary$v1 <- as.factor(summary$v1)
summary$v2 <- as.factor(summary$v2)

ggplot(summary, aes(v1, v3, fill = v2)) +
  geom_boxplot(width = 1, size = 0.4) +
  geom_dotplot(binaxis = "y", stackdir = "center", 
               binwidth = 5, position = position_dodge(1)) +
  facet_wrap(~ v1, scales = "free_x") + 
  theme(
    strip.background = element_blank(),
    strip.text.x = element_blank()
  )

reprex 包于 2022-06-06 创建 (v2.0.1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM