如何在R中的分组箱图中手动增加两个特定箱之间的间距? [英] How to manually increase spacing between two specific boxes within a grouped box plot in R?

查看:118
本文介绍了如何在R中的分组箱图中手动增加两个特定箱之间的间距?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法增加此箱形图中黄色和红色方框之间的间距?

Is there a way to increase the spacing between the yellow and red boxes within this box plot?

set.seed(40)
df <- data.frame(
  Outcome = runif(60), 
  Fruit = rep(1:3, each = 10), 
  Freshness = rep(c(0, 0.5), each = 30), 
  Farm = factor(rep(c("A", "B"), each = 5))
) %>% 
  transform(
  Outcome = Outcome*Fruit+Freshness, 
  Fruit = as.factor(Fruit), 
  Freshness = as.factor(Freshness)
)

ggplot(data = df, aes(Farm, Outcome, col = Freshness, fill = Fruit)) + 
  geom_boxplot() + 
  scale_color_manual(values = c("lightslategrey", "black"), labels = c("Stale", "Fresh")) + 
  scale_fill_manual(values = c("red", "orange", "yellow"), labels = c("Apples", "Oranges", "Bananas"))

我想增加每个"Farm"组中"Freshness"颜色组之间的间距(或留一个间隙),但不要太大,以至于框与"Farm"组一样分开.也就是说,我只想增加黄色框和红色框之间的间距,以强调新鲜度"组之间的差异.

I want to increase the spacing (or put a gap) between the "Freshness" color groups within each "Farm" group, but not so much that the boxes would be as separated as the "Farm" groups. That is, I only want to increase the spacing between yellow and red boxes to emphasize the difference between "Freshness" groups.

水果"组仍将保持组内各个框之间的距离.也就是说,相邻的红色,橙色和黄色框将保持靠近.

The "Fruit" groups would still maintain their distance between boxes within the group. That is, the neighboring red, orange, and yellow boxes will remain close together.

推荐答案

在这里您有被黑客入侵的地方:

Here you have a hack:

已完成以下操作:

  • 创建新的 Fruit ,其值超出范围(在这种情况下为负)
  • 将ylim限制为正值,以防止在情节中显示这种新果实
  • 捏造图例不显示这种结果(尽管空间仍然保持不变)
  • 隐藏图例背景以使图例中的空白不显示
  • create new Fruit with values outside of range (negative, in this case)
  • restrict ylim to positive values, as to prevent showing this new fruit in the plot
  • fudge legend to not show this fruit (space still remains same, though)
  • hide legend background to make this empty space in legend not show up
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)
set.seed(40)
df <- data.frame(
  Outcome = runif(60), 
  Fruit = rep(1:3, each = 10), 
  Freshness = rep(c(0, 0.5), each = 30), 
  Farm = factor(rep(c("A", "B"), each = 5))
) %>% 
  transform(
    Outcome = Outcome*Fruit+Freshness, 
    Fruit = as.factor(Fruit), 
    Freshness = as.factor(Freshness)
  )

dfe <- data.frame(
    Outcome = rep(-1,2), 
    Fruit = rep(" ", 2), 
    Freshness = c(0, 0),
    Farm=c("A", "B")
  )

df1 <- rbind(df, dfe) %>% 
  mutate(Fruit = factor(Fruit, levels=unique(Fruit)), 
         Freshness = factor(Freshness, levels=unique(Freshness)))

ggplot(data = df1, aes(Farm, Outcome, col = Freshness, fill = Fruit)) + 
  geom_boxplot(position=position_dodge2(padding=.1)) + 
  scale_color_manual(values = c("lightslategrey", "black"), labels = c("Stale", "Fresh")) + 
  scale_fill_manual(values = c("red", "orange", "yellow", "white"), labels = c("Apples", "Oranges", "Bananas", ""))+
  scale_x_discrete(drop=FALSE) + coord_cartesian(ylim=c(0,max(df1$Outcome)))+
  guides(fill = guide_legend(override.aes = list(size = c(rep(.6, 3), 0), fill = c("red", "orange", "yellow", NA))))+
  theme(legend.key = element_rect(fill = "white"))

reprex软件包(v0.3.0)创建于2020-05-26 sup>

Created on 2020-05-26 by the reprex package (v0.3.0)

这篇关于如何在R中的分组箱图中手动增加两个特定箱之间的间距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆