Home > Enterprise >  Error to generate graph in R: 'x' and 'y' lengths differ
Error to generate graph in R: 'x' and 'y' lengths differ

Time:10-10

Could you help me generate the graph from the code below. It's giving the following error: Error in xy.coords(x, y) : 'x' and 'y' lengths differ. Need to tweak something in graph generation!

Thanks for your help!

library(dplyr)

df <- structure(
  list(date = c("2021-06-30","2021-06-30","2021-07-07","2021-07-07","2021-07-09","2021-07-09","2021-07-09","2021-07-16"),
       Code = c("ABC","BCD","ABC","BCD","DCE","CDE","DCE","CDE"),
       Week= c("Wednesday","Wednesday","Wednesday","Wednesday","Friday","Friday","Friday","Friday"),
       DR1 = c(4,1,4,3,3,4,3,5)),
  class = "data.frame", row.names = c(NA, -8L))

dmda<-"2021-06-30"
CodeChosse<-"ABC"

datas <- structure(
  list(Code = c("ABC","ABC"),
       Days = c(11,12),
       Numbers = c(11,12)),
  class = "data.frame", row.names = c(NA, -2L))

f1 <- function(dat, code_nm) {
  dat <- subset(dat,  Code == code_nm)
  
  mxrange <-  range(datas$Numbers) 
  mxrange[2] <- mxrange[2] - (mxrange[2] %%10)   10
  max<-max(datas$Days, na.rm = TRUE) 1
  dmda<-paste(rev(unlist(strsplit(dmda, "-", fixed = TRUE))), collapse = "/")
  
  plot(Numbers ~ Days,  xlim= c(0,max), ylim= c(0,mxrange[2]),
       xaxs='i',data = dat,main = paste0(dmda, "-", code_nm))

    if (nrow(dat)<=2){
    
  m<-df %>%
    group_by(Code,Week) %>%
    summarize(across(starts_with("DR1"), mean))
  
  m <- subset(m, Week == df$Week[match(dmda, df$date)] & Code == CodeChosse)$DR1
  
   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")
  }
  
  }

f1(datas, CodeChosse)
> f1(datas, CodeChosse)
 Error in xy.coords(x, y) : 'x' and 'y' lengths differ 

If you do m outside the function, it works, from what I've seen, but I wanted to leave it inside the function.

m<-df %>%
  group_by(Code,Week) %>%
  summarize(across(starts_with("DR1"), mean))

m <- subset(m, Week == df$Week[match(dmda, df$date)] & Code == CodeChosse)$DR1


f1 <- function(dat, code_nm) {
  dat <- subset(dat,  Code == code_nm)
  
  mxrange <-  range(datas$Numbers) 
  mxrange[2] <- mxrange[2] - (mxrange[2] %%10)   10
  max<-max(datas$Days, na.rm = TRUE) 1
  dmda<-paste(rev(unlist(strsplit(dmda, "-", fixed = TRUE))), collapse = "/")
  
  plot(Numbers ~ Days,  xlim= c(0,max), ylim= c(0,mxrange[2]),
       xaxs='i',data = dat,main = paste0(dmda, "-", code_nm))
  
  if (nrow(dat)<=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")
  }
  
}

f1(datas, CodeChosse)

enter image description here

CodePudding user response:

The issue seems to be at subset level where the dmda created is of format -dd/mm/YYYY and the format in 'date' column of 'df' is YYYY-MM-DD. If we change the format of either one of them, it works in ==

f1 <- function(dat, code_nm) {
  dat <- subset(dat,  Code == code_nm)
  
  mxrange <-  range(datas$Numbers) 
  mxrange[2] <- mxrange[2] - (mxrange[2] %%10)   10
  max<-max(datas$Days, na.rm = TRUE) 1
  dmda<-paste(rev(unlist(strsplit(dmda, "-", fixed = TRUE))), collapse = "/")
  
  plot(Numbers ~ Days,  xlim= c(0,max), ylim= c(0,mxrange[2]),
       xaxs='i',data = dat,main = paste0(dmda, "-", code_nm))

    if (nrow(dat)<=2){
    
  m<-df %>%
    group_by(Code,Week) %>%
    summarize(across(starts_with("DR1"), mean), .groups = 'drop')
  
  
  m <- subset(m, Week == df$Week[match(dmda, format(as.Date(df$date), "%d/%m/%Y"))] & Code == CodeChosse)$DR1
 
   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")
  } 
  
  }

-testing

f1(datas,  CodeChosse)

-output

enter image description here

  • Related