R 当N很小时,在geom_boxplot()中隐藏框和胡须

R 当N很小时,在geom_boxplot()中隐藏框和胡须,r,ggplot2,boxplot,R,Ggplot2,Boxplot,我经常制作箱线图,其中一些类别非常小,而另一些类别有大量的数据,并叠加了抖动的原始数据点。我正在寻找一种可靠的方法来隐藏非常小的类别的框和胡须N好的,我不认为这种方法一定比您当前的选择更好,但是。。。您可以将df拆分为箱线图和散点图的df,并修改要从箱线图中删除的数据的值,使其超出范围,例如,此处为1000。然后绘制两者,最后使用coord_cartesian放大相关部分 为了创建df_框,我们按组和性别分组,并将

我经常制作箱线图,其中一些类别非常小,而另一些类别有大量的数据,并叠加了抖动的原始数据点。我正在寻找一种可靠的方法来隐藏非常小的类别的框和胡须N好的,我不认为这种方法一定比您当前的选择更好,但是。。。您可以将df拆分为箱线图和散点图的df,并修改要从箱线图中删除的数据的值,使其超出范围,例如,此处为1000。然后绘制两者,最后使用coord_cartesian放大相关部分

为了创建df_框,我们按组和性别分组,并将<5个数据点的组的值更改为1000,这样我们就不必硬编码要更改的值

df <- data.frame(group=factor(sample(c("A","B"), size=110, replace=TRUE)),
                 sex=factor(c(rep("M",50), rep("F", 50), rep("NB", 10))),
                 height=c(rnorm(50, 70, 6), rnorm(50, 63, 6), rnorm(10, 65, 6)))

df_box <- df %>%
    group_by(group, sex) %>%
    mutate(temp = ifelse(n() < 5, 1000, 1)) %>%
    ungroup() %>%
    mutate(height = ifelse(temp == 1000, 1000, height)) %>%
    select(-temp)

ggplot(df, aes(x=group, y=height, colour=sex)) +
    geom_boxplot(data=df_box) +
    geom_point(position=position_jitterdodge(jitter.width=0.2)) +
    coord_cartesian(ylim=c(50,90))

我为您的身高数据制作了第二列,其中小样本组的值替换为NA。打印数据时,将原始高度列用作点的y美学,将带有NA值的新列用作小编组的y美学

要使箱线图和点正确对齐,请使用geom_BoxPlot Position_dodgepreserve=single告诉ggplot即使缺少数据也要保持箱线图的恒定宽度

require(tidyverse)

df <- data.frame(group = factor(sample(c("A", "B"), size = 110, replace = TRUE)),
                 sex = factor(c(rep("M", 50), rep("F", 50), rep("NB", 10))),
                 height = c(rnorm(50, 70, 6), rnorm(50, 63, 6), rnorm(10, 65, 6)))

n <- df %>% #calculate sample sizes
  group_by(group, sex) %>%
  summarize(n = n())

df <- left_join(df, n) %>% #join sample sizes to df
  #make second height column to use for boxplots: NA values if n is too small
  mutate(boxplot_height = ifelse(n < 5, NA, height)) 


ggplot(df, aes(x = group, colour = sex)) +
  #use height column that has groups with n < 5 coded as NA to plot boxplots
  geom_boxplot(aes(y = boxplot_height),
               #preserve = "single" maintains constant width of boxes 
               position = position_dodge(preserve = "single")) +
  geom_point(aes(y = height), #use all height data as y variable for points
             position = position_jitterdodge(jitter.width = 0.2))

这是伟大的,如此接近。弱点在于,只有在道奇处理的任何因素中,小类别总是最后一个时,它才会这样工作。对于我目前的问题,我可以做到这一点,但我可以想象这样做不可行的情况,例如,在上述数据集中,如果a组中几乎没有男性,但B组中几乎没有女性。这很令人沮丧,我没有看到原始数据集存在这一问题!我做了一个快速搜索,不幸的是,这似乎是一个缺乏功能的问题,目前,重新排序您的因子以使用我的解决方案或使用@heds1解决方案可能是您最好的选择。@JanBoyer我喜欢您的解决方案,尤其是我不知道的preserve='single'!这就是我将如何解决它,但不会创建新的数据帧。只需修改df,将高度_框添加为一列,并更改方框图中的y美学。
require(tidyverse)

df <- data.frame(group = factor(sample(c("A", "B"), size = 110, replace = TRUE)),
                 sex = factor(c(rep("M", 50), rep("F", 50), rep("NB", 10))),
                 height = c(rnorm(50, 70, 6), rnorm(50, 63, 6), rnorm(10, 65, 6)))

n <- df %>% #calculate sample sizes
  group_by(group, sex) %>%
  summarize(n = n())

df <- left_join(df, n) %>% #join sample sizes to df
  #make second height column to use for boxplots: NA values if n is too small
  mutate(boxplot_height = ifelse(n < 5, NA, height)) 


ggplot(df, aes(x = group, colour = sex)) +
  #use height column that has groups with n < 5 coded as NA to plot boxplots
  geom_boxplot(aes(y = boxplot_height),
               #preserve = "single" maintains constant width of boxes 
               position = position_dodge(preserve = "single")) +
  geom_point(aes(y = height), #use all height data as y variable for points
             position = position_jitterdodge(jitter.width = 0.2))