【问题标题】:Is it possible to cut the y-axis for a one row of facets only?是否可以仅切割一排刻面的 y 轴?
【发布时间】:2020-07-16 13:37:01
【问题描述】:

我有一个这样的数据框:

   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 尺度的限制,所有扩展这些限制的数据都会丢失。

【问题讨论】:

    标签: r ggplot2 facet


    【解决方案1】:

    是的,这可以通过将 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,这不是一个好方法,因为“大于 8” -check 不能很好地区分您的案例。

    【讨论】:

    • 谢谢,太好了!如果 oob_keep 不起作用,我做错了什么? "错误:'oob_keep' 不是从 'namespace:scales' 导出的对象"
    • 您可以将音阶更新到最新版本,也可以改用function(x, ...){x}
    • 你是个天才!非常感谢您的帮助和时间
    【解决方案2】:

    你的意思是这样的。我已经添加了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 开头,以 11(或 15)结尾,这样可以更容易地看出这个区间的差异。所以基本上从这些方面的情节中删除低于 8 的所有内容。像“axis.breaks”之类的东西,我已经尝试过,但到目前为止还无法使用方面
    • @BeccaLi 我希望这对您的问题有用:)
    • 很抱歉我的描述能力不好,我希望我的问题现在更清楚了。再次感谢!
    • @BeccaLi 有可能给我一分钟!
    猜你喜欢
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多