在ggplot2中使用Squash_axis且数据集没有NA值时,出现错误“下标分配中不允许使用NA”

马雷捷·阿德里亚斯(Marretje Adriaanse)

我想跳过大部分-10到100之间的数据集的y轴部分,然后再跳过一些400的值。所以我想挤压这个空白区域。我已经在我的绘图中使用了Facet网格用于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)
                 )

然后壁球轴功能:

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,示例数据中也没有NA。

到底是怎么回事?

斯蒂芬

使用browser()if找出问题是在inv压缩转换一部分中出现的但是我只能猜测原因是什么,可能与中断的设置方式(??)有关。但是,scale_y_continuous我没有通过内部应用转换,而是尝试通过coord_trans... et voila应用了转换

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软件包(v0.3.0)创建于2020-04-03

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

日期错误 下标作业中不允许使用 NA

多项式回归中的错误:“下标分配中不允许使用NA”

在不适当的情况下分配“下标分配中不允许使用NA”

“在下标的作业中不允许使用NA”

下标作业中不允许使用NA

当any(is.na(counts))= FALSE时,DESeq2“不允许使用NA值”错误

尝试替换值时,“在数据帧的下标分配中不允许丢失值”

下标作业中不允许R预测NA

构建trpl-ebook时遇到错误:没有主体的方法中不允许使用模式

错误:运行setup:upgrade时,事务中不允许使用DDL语句。运行数据补丁

使用 Windows 服务中托管的 WCF 服务的 Winform 出现错误 #405(方法不允许 e)

Redshift错误中不允许使用DATEADD函数

为什么ArrayDeque中不允许使用空值?

CloudFormation错误[/资源]模板中不允许使用'null'值

没有/ <id>的请求中不允许使用方法PUT

使用POST与@FormParam(带有Jersey REST的Java Web服务)使用405时出现“方法不允许”错误

发生错误:创建PSQL变量时“在过程中不允许使用GRANT OPTION”

列中不允许使用空值-旧版数据库

私有继承中不允许使用基础对象

Apache2中不允许使用符号链接

让!不允许在seq {...}中异步使用?

Rails字符串不允许在创建时在数据库中使用空字符串值

Apache:使用 If 条件时出现“此处不允许使用虚拟主机”错误

使用Administrate时添加请求参数时出现不允许的参数错误

火山图错误(使用ggplot2):绘制时没有数据

在XML模式中使用断言在特定条件满足时不允许节点出现

尝试在Spring Boot / Spring Security Web应用程序中打开.css文件时,为什么会出现“ 405-不允许使用方法”错误?

R studio不能使用ggplot2且没有错误消息

在ggplot2中使用pmap自动绘制不同数据集的图形