在 ggplot2 中使用 Squash_axis 时出现错误 "NAs are not allowed in subscripted assignments",数据集没有 NA 值

Error "NAs are not allowed in subscripted assignments" while using Squash_axis in ggplot2, with dataset without NA-values

对于大多数值在 -10 到 100 之间的数据集,我想跳过我的部分 y 轴,然后再次在 400 之间跳过一些。所以我想挤压这个空白区域。我已经在 3 种不同场景的情节中使用了小平面网格,所以我更愿意只 "squash" Y 轴而不创建多个情节。

我在 RPubs (https://rpubs.com/huanfaChen/squash_remove_y_axix_ggplot_) 上找到了 "squash_axis" 功能,也许可以帮到我。但是我无法让它与我自己的数据集一起使用,甚至无法与示例数据集一起使用。

示例数据集(我的看起来很相似,除了有另一列时间)

dat <- data.frame(group=rep(c('A', 'B', 'C', 'D'), each = 10), 
                 value=c(rnorm(10), rnorm(10)+100)
                 )

然后是Squash轴函数:

require(ggplot2)
squash_axis <- function(from, to, factor) { 
    # A transformation function that squashes the range of [from, to] by factor on a given axis 

    # Args:
    #   from: left end of the axis
    #   to: right end of the axis
    #   factor: the compression factor of the range [from, to]
    #
    # Returns:
    #   A transformation called "squash_axis", which is capsulated by trans_new() function

  trans <- function(x) {    
      # get indices for the relevant regions
      isq <- x > from & x < to
      ito <- x >= to

      # apply transformation
      x[isq] <- from + (x[isq] - from)/factor
      x[ito] <- from + (to - from)/factor + (x[ito] - to)

      return(x)
  }

  inv <- function(x) {

      # get indices for the relevant regions
      isq <- x > from & x < from + (to - from)/factor
      ito <- x >= from + (to - from)/factor

      # apply transformation
      x[isq] <- from + (x[isq] - from) * factor
      x[ito] <- to + (x[ito] - (from + (to - from)/factor))

      return(x)
  }

# return the transformation
  return(trans_new("squash_axis", trans, inv))
}

示例中的情节:

ggplot(dat,aes(x=group,y=value))+
  geom_point()+
  scale_y_continuous(trans = squash_axis(5, 95, 10))

然后我得到错误: x[isq] <- from + (x[isq] - from) * factor 错误: 下标作业中不允许使用 NA

我不明白,因为我的数据中没有 NA,示例数据中也没有。

这是怎么回事?

Using browser() if 发现问题出在挤压变换的inv部分。但我只能猜测原因是什么,可能与中断的设置方式有关(??)。但是,我没有在 scale_y_continuous 中应用转换,而是尝试通过 coord_trans 应用它 ... 瞧,成功了:

library(ggplot2)

dat <- data.frame(group=rep(c('A', 'B', 'C', 'D'), each = 10), 
                  value=c(rnorm(10), rnorm(10)+100)
)

squash_axis <- function(from, to, factor) { 
  # A transformation function that squashes the range of [from, to] by factor on a given axis 

  # Args:
  #   from: left end of the axis
  #   to: right end of the axis
  #   factor: the compression factor of the range [from, to]
  #
  # Returns:
  #   A transformation called "squash_axis", which is capsulated by trans_new() function

  trans <- function(x) {    
    # get indices for the relevant regions
    isq <- x > from & x < to
    ito <- x >= to

    # apply transformation
    x[isq] <- from + (x[isq] - from)/factor
    x[ito] <- from + (to - from)/factor + (x[ito] - to)

    return(x)
  }

  inv <- function(x) {

    # get indices for the relevant regions
    isq <- x > from & x < from + (to - from)/factor
    ito <- x >= from + (to - from)/factor

    # apply transformation
    x[isq] <- from + (x[isq] - from) * factor
    x[ito] <- to + (x[ito] - (from + (to - from)/factor))

    return(x)
  }

  # return the transformation
  return(scales::trans_new("squash_axis", trans, inv, domain = c(from, to)))
}

ggplot(dat,aes(x=group, y = value))+
  geom_point()+
  coord_trans(y = squash_axis(5, 95, 10))

reprex package (v0.3.0)

于 2020-04-03 创建