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

标签 r ggplot2 skip yaxis

对于大多数值在 -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] <- from + (x[isq] - from) * factor 中的错误:
下标赋值中不允许使用 NA

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

到底是怎么回事?

最佳答案

使用 browser()如果发现问题出在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))



创建于 2020-04-03 由 reprex package (v0.3.0)

关于r - 在 ggplot2 中使用 Squash_axis 时出现错误 "NAs are not allowed in subscripted assignments",数据集没有 NA 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61010786/

相关文章:

r - R 中的增量时间戳解析(纳秒、微秒、毫秒)

r - 将不同的函数应用于嵌套列表中的不同元素

r - ggplot 保留组合图的重复颜色

c++ - 如何跳过基于范围的 for 循环中基于 'index' 的元素?

r - read.csv,第一行的标题,跳过第二行

java - rcaller 2.2 和 2.3 不会通过 runAndReturnResultOnline 在顺序命令模式下停止

css - 图像作为带有标题叠加的 Shiny 头部背景

r - 两个 geom_bar() 和因子变量的错误顺序

r - 如何在网格中绘制绘图列表

Java AudioInputStream如何支持跳过负字节数