【问题标题】:Error "NAs are not allowed in subscripted assignments" while using Squash_axis in ggplot2, with dataset without NA-values在 ggplot2 中使用 Squash_axis 时出现错误“下标分配中不允许使用 NA”,数据集没有 NA 值
【发布时间】:2020-07-15 13:23:36
【问题描述】:

对于大多数值在 -10 到 100 之间的数据集,我想跳过部分 y 轴,然后又在 400 处跳过一些。所以我想挤压这个空白区域。我已经在我的情节中为 3 个不同的场景使用了分面网格,所以我宁愿只“挤压”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]

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

发生了什么事?

【问题讨论】:

    标签: r ggplot2 skip yaxis


    【解决方案1】:

    如果发现问题出在压缩转换的inv 部分,则使用browser()。但我只能猜测原因是什么,可能与休息时间的设置方式有关(??)。但是,我没有在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 年 4 月 3 日创建

    【讨论】:

      猜你喜欢
      • 2017-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-20
      • 1970-01-01
      • 2019-04-06
      相关资源
      最近更新 更多