是否可以只为一排小平面切割 y 轴?

Is it possible to cut the y-axis for a one row of facets only?

我有一个这样的数据框:

   Value Time Colour Fruit
1    9.0    1    Red Apple
2    9.5    2    Red Apple
3   10.0    3    Red Apple
4    9.0    1   Blue Apple
5    9.5    2   Blue Apple
6   10.0    3   Blue Apple
7    1.0    1    Red  Pear
8    2.0    2    Red  Pear
9    3.0    3    Red  Pear
10   2.0    1   Blue  Pear
11   1.0    2   Blue  Pear
12   3.0    3   Blue  Pear

和以下情节:

Plotprep <- ggplot(Data, aes(x=`Time`, y=Value, fill= Time))

Plotprep +
  geom_bar(position= position_dodge2(aes(fill=Time)), stat="identity", show.legend=FALSE)+
  ylab("Value") + xlab("Colour") +
  facet_grid(`Fruit`~`Colour`, scales = "free", space = "free_x", switch="y") +
  scale_y_continuous() + 
  theme(strip.background = element_blank(), 
        axis.text.x=element_text(),
        axis.line = element_line(colour = "black", linetype = "solid"))

很难看出顶行刻面中条形之间的差异 因为与酒吧尺寸相比,它们相对较小。有没有办法,我只能在这一行中调整 y 比例以显示 8 到 11 之间的部分? (有点像我可以放大一些图形程序)

我遇到的两个问题是:

  1. 我尝试的一切都用于所有方面;
  2. 如果我使用 y 尺度的限制,所有扩展这些限制的数据都会丢失。

你的意思是这样的。我添加了 15 作为示例目的(您可以更改为 11)并使用 geom_blank() 和那个虚拟比例。

library(ggplot2)
#Mutate
Data2 <- Data %>% mutate(ymax=ifelse(Fruit=='Apple',15,Value))

#Plot
Plotprep <- ggplot(Data2, aes(x=`Time`, y=Value, fill= Time))

Plotprep +
  geom_bar(position= position_dodge2(aes(fill=Time)), stat="identity", show.legend=FALSE)+
  ylab("Value") + xlab("Colour") +
  facet_grid(`Fruit`~`Colour`, scales = "free", space = "free_x", switch="y") +
  scale_y_continuous(limits = c(0,NA),n.breaks = 8) +
  geom_blank(aes(y = ymax))+
  theme(strip.background = element_blank(), 
        axis.text.x=element_text(),
        axis.line = element_line(colour = "black", 
                                 linetype = "solid"))

是的,这可以通过将 y 轴的限制设置为函数来实现。以下检查最大限制是否大于 8,如果是,则将 y 轴切割为 8。您还需要专门针对条形图控制 oob 参数,因为它们在内部被参数化为矩形(因此它们的 ymin 将越界)。

library(ggplot2)

zz <- "
   Value Time Colour Fruit
1    9.0    1    Red Apple
2    9.5    2    Red Apple
3   10.0    3    Red Apple
4    9.0    1   Blue Apple
5    9.5    2   Blue Apple
6   10.0    3   Blue Apple
7    1.0    1    Red  Pear
8    2.0    2    Red  Pear
9    3.0    3    Red  Pear
10   2.0    1   Blue  Pear
11   1.0    2   Blue  Pear
12   3.0    3   Blue  Pear"

Data <- read.table(text = zz)

Plotprep <- ggplot(Data, aes(x=`Time`, y=Value, fill= Time))


Plotprep +
  geom_bar(position= position_dodge2(aes(fill=Time)), stat="identity", show.legend=FALSE)+
  ylab("Value") + xlab("Colour") +
  facet_grid(`Fruit`~`Colour`, scales = "free", space = "free_x", switch="y") +
  scale_y_continuous(
    limits = function(x) {
      if (x[2] > 8) {
        return(c(8, x[2]))
      } else {
        return(x)
      }
    }, oob = scales::oob_keep
  ) + 
  theme(strip.background = element_blank(), 
        axis.text.x=element_text(),
        axis.line = element_line(colour = "black", 
                                 linetype = "solid"))

我还需要补充一点,如果例如底行的范围是 0-15 而顶部的范围是 7-9,这不是一个好方法,因为 'is-larger-than-eight'-check没有很好地分开你的案件。