R:在函数中需要有限的“ylim"值 [英] R: need finite 'ylim' values in function

查看:41
本文介绍了R:在函数中需要有限的“ylim"值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 data.frame xy 中为每个组(由 ID 定义)绘制数据.当 1946 年之前的一年在一个组中时,应该执行 plot 2.当年份在 1946 和 2014 之间时,应该执行 plot1.

I'd like to plot the data in data.frame xy for each group (defined by ID). When a year before 1946 is in a group, plot 2 should be executed. When the years are between 1946 and 2014, plot1 should be executed.

我的问题:如果没有 NA 值,这可以正常工作,但是由于我有数据间隙,我依靠 NA 来定义这些数据间隙.这就是我收到错误的原因:plot.window 中的错误(需要有限的 'ylim' 值).我试图将 finite=T 放在 y 轴的 plot1 中,但这会导致 subscript out of bounds 错误.有没有办法可以解决这个问题并且正确绘制图形?

My problem: This works fine without NA values, but as I have data gaps I rely on NAs to define these data gaps. This is why I get an error: error in plot.window(need finite 'ylim' values). I tried to put finite=T in plot1 at the y-axis but this gives a subscript out of bounds error. Is there a way I could solve this and that the graphics are correctly plotted?

以下是我的代码:它很长,但大部分代码由我所依赖的 plot() 选项组成.

In the following is my code: It is long but most of the code consists of plot() options on which I rely.

# read in sample data and split it up by group (defined by ID)
xy <- data.frame(NAME=c("NAME2","NAME2","NAME2","NAME2","NAME2","NAME3","NAME3","NAME3","NAME3","NAME5","NAME5","NAME5","NAME5"), ID=c(48,48,48,48,48,32,32,32,32,67,67,67,67),YEAR=c(1981,1983,1984,1988,1989,1984,1984,1988,1988,1899,1933,1948,1958),VALUE=c(0,205,-570,0,-310,-3680,-3680,NA,-3680,0,NA,13,-98))
ind <- split(x = xy,f = xy[,'ID'])

# Plot Scenario 1: if only years between 1946 and 2014 are present for each group do this:
  plot1 <- function(x) {
  fname <- paste0(x[1, 'ID'], '.png')
  png(fname, width=1679, height=1165, res=150)
  par(mar=c(6,8,6,5))
  plot(x = c(1946, 2014),
       y = range(x$VALUE),
       type='n',
       main=x[1, 'NAME'],
       xlab="Time [Years]",
       ylab="Value")
  axis(2, at = seq(-100000, 100000, 500), cex.axis=1, labels=FALSE, tcl=-0.3)
  points(ind[[i]][,c('YEAR','VALUE')], type="l", lwd=2)
  points(ind[[i]][,c('YEAR','VALUE')], type="p", lwd=1, cex=1,   pch=21, bg='white')
  abline(h=0)
  dev.off()
}

# Plot Scenario 2 if years under 1946 are present do this:
plot2 <- function(x) {
  fname <- paste0(x[1, 'ID'], '.png')
  png(fname, width=1679, height=1165, res=150)    
  par(mar=c(6,8,6,5))
  plot(x[,c('YEAR','VALUE')],
       type='n',
       main=x[1, 'NAME'],
  xlab="Time [Years]",
  ylab="Value [mm]")
axis(2, at = seq(-100000, 100000, 500), cex.axis=1, labels=FALSE, tcl=-0.3)
points(ind[[i]][,c('YEAR','VALUE')], type="l", lwd=2)
points(ind[[i]][,c('YEAR','VALUE')], type="p", lwd=1, cex=1,   pch=21, bg='white')
abline(h=0)
dev.off() 
}

# Execute functions
    lapply(ind, function(x) ifelse(any(x$YEAR < 1946 & x$YEAR < 2014), plot2(x), plot1(x)))

推荐答案

plot1中,将y = range(x$VALUE)改为y = range(x$VALUE, na.rm=TRUE) 删除 NA 缺失值.

In plot1, change y = range(x$VALUE) to y = range(x$VALUE, na.rm=TRUE) to remove NA missing values.

您还有另一个问题,即在两个函数中您都引用了 ind[[i]],我认为这意味着该代码在某些时候是循环的一部分.我的猜测是所有对 ind[[i]] 的引用都应该是 x.

You have another problem, namely that in both functions you refer to ind[[i]], which I assume means at some point this code was part of a loop. My guess is all the references to ind[[i]] should be x.

这篇关于R:在函数中需要有限的“ylim"值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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