【问题标题】:Error in plot.window(...) : need finite 'ylim' values to generate graph in Rplot.window(...) 中的错误:需要有限的“ylim”值才能在 R 中生成图形
【发布时间】:2021-10-13 21:24:15
【问题描述】:

您能帮我绘制 02/07 日的图表,类别 ABC。出现以下错误:Error in plot.window(...) : need finite 'ylim' values。如果您尝试为另一天/类别生成,泰国是 30/06(ABC 类别)和 01/07(ABC 类别)它有效,仅适用于 02/07 的那一天。你可以帮帮我吗?提前谢谢!

可执行代码如下:

library(dplyr)

df1 <- structure(
  list(date1= c("2021-06-28","2021-06-28","2021-06-28"),
       date2 = c("2021-06-30","2021-07-01","2021-07-02"),
       Category = c("ABC","ABC","ABC"),
       Week= c("Wednesday","Wednesday","Wednesday"),
       DR1 = c(4,1,0),
       DR01 = c(4,1,0), DR02= c(4,2,0),DR03= c(9,5,0),
       DR04 = c(5,4,0),DR05 = c(5,4,0)),
  class = "data.frame", row.names = c(NA, -3L))


f1 <- function(dmda, CategoryChosse) {
  
  x<-df1 %>% select(starts_with("DR0"))
  
  x<-cbind(df1, setNames(df1$DR1 - x, paste0(names(x), "_PV")))
  PV<-select(x, date2,Week, Category, DR1, ends_with("PV"))
  
  med<-PV %>%
    group_by(Category,Week) %>%
    summarize(across(ends_with("PV"), median))
  
  SPV<-df1%>%
    inner_join(med, by = c('Category', 'Week')) %>%
    mutate(across(matches("^DR0\\d+$"), ~.x + 
                    get(paste0(cur_column(), '_PV')),
                  .names = '{col}_{col}_PV')) %>%
    select(date1:Category, DR01_DR01_PV:last_col())
  
  SPV<-data.frame(SPV)
  
  mat1 <- df1 %>%
    filter(date2 == dmda, Category == CategoryChosse) %>%
    select(starts_with("DR0")) %>%
    pivot_longer(cols = everything()) %>%
    arrange(desc(row_number())) %>%
    mutate(cs = cumsum(value)) %>%
    filter(cs == 0) %>%
    pull(name)
  
  (dropnames <- paste0(mat1,"_",mat1, "_PV"))
  
  SPV <- SPV %>%
    filter(date2 == dmda, Category == CategoryChosse) %>%
    select(-any_of(dropnames))
  
  if(length(grep("DR0", names(SPV))) == 0) {
    SPV[mat1] <- NA_real_
  }
  
  datas <-SPV %>%
    filter(date2 == ymd(dmda)) %>%
    group_by(Category) %>%
    summarize(across(starts_with("DR0"), sum)) %>%
    pivot_longer(cols= -Category, names_pattern = "DR0(.+)", values_to = "val") %>%
    mutate(name = readr::parse_number(name))
  colnames(datas)[-1]<-c("Days","Numbers")
  
  
  datas <- datas %>% 
    group_by(Category) %>% 
    slice((as.Date(dmda) - min(as.Date(df1$date1) [
      df1$Category == first(Category)])):max(Days)+1) %>%
    ungroup
  
  m<-df1 %>%
    group_by(Category,Week) %>%
    summarize(across(starts_with("DR1"), mean))
  
  m<-subset(m, Week == df1$Week[match(ymd(dmda), ymd(df1$date2))] & Category == CategoryChosse)$DR1
  
  maxrange <-  range(datas$Numbers, na.rm = TRUE)
  maxrange[2] <- maxrange[2] - (maxrange[2] %%10) + 35
  
  max<-max(datas$Days, na.rm = TRUE)+1

    plot(Numbers ~ Days,  xlim= c(0,max),  ylim= c(0,maxrange[2]),
       xaxs='i',data = datas,main = paste0(dmda, "-", CategoryChosse))

  if (nrow(datas)<=2){
    abline(h=m,lwd=2) 
    points(0, m, col = "red", pch = 19, cex = 2, xpd = TRUE)
    text(.1,m+ .5, round(m,1), cex=1.1,pos=4,offset =1,col="black")}
  
  else if(any(table(datas$Numbers) >= 3) & length(unique(datas$Numbers)) == 1){
    yz <- unique(datas$Numbers)
    lines(c(0,datas$Days), c(yz, datas$Numbers), lwd = 2)
    points(0, yz, col = "red", pch = 19, cex = 2, xpd = TRUE)
    text(.1,yz+ .5,round(yz,1), cex=1.1,pos=4,offset =1,col="black")}
  
  else{
    mod <- nls(Numbers ~ b1*Days^2+b2,start = list(b1 = 0,b2 = 0),data = datas, algorithm = "port")
    new.data <- data.frame(Days = with(datas, seq(min(Days),max(Days),len = 45)))
    new.data <- rbind(0, new.data)
    lines(new.data$Days,predict(mod,newdata = new.data),lwd=2)
    coef<-coef(mod)[2]
    points(0, coef, col="red",pch=19,cex = 2,xpd=TRUE)
    text(.99,coef + 1,max(0, round(coef,1)), cex=1.1,pos=4,offset =1,col="black")
  }
  
}


 f1("2021-06-30", "ABC")

 f1("2021-07-01", "ABC")
    
 f1("2021-07-02", "ABC")

【问题讨论】:

  • 原因是&gt; datas$Numbers# [1] NA,因此返回Inf的范围maxrange &lt;- range(datas$Numbers, na.rm = TRUE);maxrange [1] Inf -Inf。如果有NA,你想返回什么

标签: r


【解决方案1】:

当只有NA元素时,如果我们使用range/min/maxna.rm = TRUE,它返回Inf

> min(NA, na.rm = TRUE)
[1] Inf
Warning message:
In min(NA, na.rm = TRUE) : no non-missing arguments to min; returning Inf
> max(NA, na.rm = TRUE)
[1] -Inf
Warning message:
In max(NA, na.rm = TRUE) : no non-missing arguments to max; returning -Inf
> range(NA, na.rm = TRUE)
[1]  Inf -Inf
Warning messages:
1: In min(x, na.rm = na.rm) :
  no non-missing arguments to min; returning Inf
2: In max(x, na.rm = na.rm) :
  no non-missing arguments to max; returning -Inf

如果是 NA,一个选项是返回 0

...
 maxrange <-  range(min(0, datas$Numbers, na.rm = TRUE), na.rm = TRUE)
...

-完整代码

f1 <- function(dmda, CategoryChosse) {
  
  x<-df1 %>% select(starts_with("DR0"))
  
  x<-cbind(df1, setNames(df1$DR1 - x, paste0(names(x), "_PV")))
  PV<-select(x, date2,Week, Category, DR1, ends_with("PV"))
  
  med<-PV %>%
    group_by(Category,Week) %>%
    summarize(across(ends_with("PV"), median))
  
  SPV<-df1%>%
    inner_join(med, by = c('Category', 'Week')) %>%
    mutate(across(matches("^DR0\\d+$"), ~.x + 
                    get(paste0(cur_column(), '_PV')),
                  .names = '{col}_{col}_PV')) %>%
    select(date1:Category, DR01_DR01_PV:last_col())
  
  SPV<-data.frame(SPV)
  
  mat1 <- df1 %>%
    filter(date2 == dmda, Category == CategoryChosse) %>%
    select(starts_with("DR0")) %>%
    pivot_longer(cols = everything()) %>%
    arrange(desc(row_number())) %>%
    mutate(cs = cumsum(value)) %>%
    filter(cs == 0) %>%
    pull(name)
  
  (dropnames <- paste0(mat1,"_",mat1, "_PV"))
  
  SPV <- SPV %>%
    filter(date2 == dmda, Category == CategoryChosse) %>%
    select(-any_of(dropnames))
  
  if(length(grep("DR0", names(SPV))) == 0) {
    SPV[mat1] <- NA_real_
  }
  
  datas <-SPV %>%
    filter(date2 == ymd(dmda)) %>%
    group_by(Category) %>%
    summarize(across(starts_with("DR0"), sum)) %>%
    pivot_longer(cols= -Category, names_pattern = "DR0(.+)", values_to = "val") %>%
    mutate(name = readr::parse_number(name))
  colnames(datas)[-1]<-c("Days","Numbers")
  
  
  datas <- datas %>% 
    group_by(Category) %>% 
    slice((as.Date(dmda) - min(as.Date(df1$date1) [
      df1$Category == first(Category)])):max(Days)+1) %>%
    ungroup
  
  m<-df1 %>%
    group_by(Category,Week) %>%
    summarize(across(starts_with("DR1"), mean))
  
  m<-subset(m, Week == df1$Week[match(ymd(dmda), ymd(df1$date2))] & Category == CategoryChosse)$DR1
  
  maxrange <-  range(min(0, datas$Numbers, na.rm = TRUE), na.rm = TRUE)
  maxrange[2] <- maxrange[2] - (maxrange[2] %%10) + 35
  
  max<-max(datas$Days, na.rm = TRUE)+1

    plot(Numbers ~ Days,  xlim= c(0,max),  ylim= c(0,maxrange[2]),
       xaxs='i',data = datas,main = paste0(dmda, "-", CategoryChosse))

  if (nrow(datas)<=2){
    abline(h=m,lwd=2) 
    points(0, m, col = "red", pch = 19, cex = 2, xpd = TRUE)
    text(.1,m+ .5, round(m,1), cex=1.1,pos=4,offset =1,col="black")}
  
  else if(any(table(datas$Numbers) >= 3) & length(unique(datas$Numbers)) == 1){
    yz <- unique(datas$Numbers)
    lines(c(0,datas$Days), c(yz, datas$Numbers), lwd = 2)
    points(0, yz, col = "red", pch = 19, cex = 2, xpd = TRUE)
    text(.1,yz+ .5,round(yz,1), cex=1.1,pos=4,offset =1,col="black")}
  
  else{
    mod <- nls(Numbers ~ b1*Days^2+b2,start = list(b1 = 0,b2 = 0),data = datas, algorithm = "port")
    new.data <- data.frame(Days = with(datas, seq(min(Days),max(Days),len = 45)))
    new.data <- rbind(0, new.data)
    lines(new.data$Days,predict(mod,newdata = new.data),lwd=2)
    coef<-coef(mod)[2]
    points(0, coef, col="red",pch=19,cex = 2,xpd=TRUE)
    text(.99,coef + 1,max(0, round(coef,1)), cex=1.1,pos=4,offset =1,col="black")
  }
  
}

-测试

f1("2021-07-02", "ABC")

【讨论】:

  • Akrun,看看你能不能帮到我:x 轴很奇怪,因为轴表示天,它是 0.5、1.5。有没有办法调整这个或谁知道,也许在这种情况下排除这个图的 x 轴?
  • @JVieira 在plot中,我们可以指定xaxt = "n",然后使用axis来改变标签
  • @JVieira 也许this 有帮助
  • 什么时候可以@akrun,请看一下这个问题:stackoverflow.com/questions/69907443/… 很相似。非常感谢!
  • 谢谢我的朋友,我现在就去看看!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-27
  • 1970-01-01
  • 2021-12-26
  • 2014-02-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多