错误“在下标的分配中不允许使用NA".在ggplot2中使用Squash_axis时,数据集没有NA值 [英] Error "NAs are not allowed in subscripted assignments" while using Squash_axis in ggplot2, with dataset without NA-values

查看:196
本文介绍了错误“在下标的分配中不允许使用NA".在ggplot2中使用Squash_axis时,数据集没有NA值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to skip part of my y-axis for a dataset with most values between -10 and 100, and then a few at 400 again. So I want to squeeze this empty area. I already am using facet grid in my plot for 3 different scenario's, so I would prefer to just "squash" the Y axis and not create mutltiple plots.

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

I found the "squash_axis" function on RPubs (https://rpubs.com/huanfaChen/squash_remove_y_axix_ggplot_), which may be able to help me. But I can not get it to work with my own dataset, and not even with the example dataset.

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

Example dataset (mine looks quite similar except that there is another column with time)

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]-的)*因子: 下标作业中不允许使用NAs

I then get the error: Error in x[isq] <- from + (x[isq] - from) * factor : NAs are not allowed in subscripted assignments

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

I don't understand because there are no NAs in my data and not in the example data either.

这是怎么回事?

推荐答案

如果发现问题是由于压缩转换的inv部分引起的,则使用browser().但是我只能猜测原因是什么,可能与中断的设置方式(??)有关.但是,我没有通过coord_trans来应用转换,而是尝试通过coord_trans来应用它,等等,有效:

Using browser() if figured out that the problem arises in the inv part of the squish transformation. But I could only guess what the reason is, probably has to do with how the breaks are set (??). However, instead of applying the transformation inside scale_y_continuous I tried applying it via coord_trans ... et voila, worked:

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 sup>

Created on 2020-04-03 by the reprex package (v0.3.0)

这篇关于错误“在下标的分配中不允许使用NA".在ggplot2中使用Squash_axis时,数据集没有NA值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆