Error in abline function when I plot

Could you please test the code below to see if the horizontal (abline) lines are working for you? In my case it doesn't show up, but I don't see any apparent errors. Is it missing a package update or something?. I've already looked, but I didn't find the reason.

library(dplyr)
library(tidyr)
library(lubridate)
library(ggplot2)

df <- structure(
list(Id=c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1),
date1 = c("2021-07-20","2021-07-20","2021-07-20","2021-07-20","2021-07-20",
"2021-07-20","2021-07-20","2021-07-20","2021-07-20","2021-07-20","2021-07-20",
"2021-07-20","2021-07-20","2021-07-20","2021-07-20","2021-07-20","2021-07-20",
"2021-07-20","2021-07-20","2021-07-20","2021-07-20"),
date2 = c("2021-07-01","2021-07-01","2021-07-01","2021-07-01","2021-04-02",
"2021-04-02","2021-04-02","2021-04-02","2021-04-02","2021-04-02","2021-04-03",
"2021-04-03","2021-04-03","2021-04-03","2021-04-03","2021-04-08","2021-04-08",
"2021-04-09","2021-04-09","2021-04-10","2021-04-10"),
Week= c("Thursday","Thursday","Thursday","Thursday","Friday","Friday","Friday","Friday",
"Friday","Friday","Saturday","Saturday","Saturday","Saturday","Saturday","Thursday",
"Thursday","Friday","Friday","Saturday","Saturday"),
D = c("","","Ho","","","","","","Ho","","","","","","","","","","","",""),
D1 = c(8,1,9, 3,5,4,7,6,3,8,2,3,4,6,7,8,4,2,6,2,3), DR01 = c(4,1,4,3,3,4,3,6,3,7,2,3,4,6,7,8,4,2,6,7,3),
DR2 = c(2,1,4,3,3,4,1,6,3,7,2,3,4,6,7,8,4,2,6,2,3), DR03 = c(7,5,4,3,3,4,1,5,3,3,2,3,4,6,7,8,4,2,6,4,3)),
class = "data.frame", row.names = c(NA, -21L))

df<-subset(df,df$date2<df$date1)

dim_data<-dim(df)

day<-c(seq.Date(from = as.Date(df$date2[1]), by = "days",
length = dim_data[1]
)) ###<--------- issue here

df_grouped <- df %>%
mutate(across(starts_with("date"), as.Date)) %>%
group_by(date2) %>%
summarise(Id = first(Id),
date1 = first(date1),
Week = first(Week),
D = first(D),
D1 = sum(D1)) %>%
select(Id,date1,date2,Week,D,D1)

df_grouped <- df_grouped %>% mutate(date1=format(date1,"%d/%m/%Y"),
date2=format(date2,"%d/%m/%Y"))
df_grouped<-data.frame(df_grouped)

#create scatter plot
scatter_date <- function(dt, dta = df) {

# get the week day
my_day <- weekdays(as.Date(dt))

df_OC<-subset(df_grouped,is.na(D) | D=="") ###<-----------  issue here
ds_OC<-subset(df_OC,df_OC$Week==my_day) 


mean_Week<-mean(as.numeric(ds_OC[,"D1"]) )
sdeviation_Week<-sd(as.numeric(ds_OC[,"D1"]))


mean_Week_pos <- (mean_Week + sdeviation_Week)
mean_Week_neg <- (mean_Week - sdeviation_Week)

dta %>%
  filter(date2 == ymd(dt)) %>%
  summarize(across(starts_with("DR"), sum)) %>%
  pivot_longer(everything(), names_pattern = "DR(.+)", values_to = "val") %>%
  mutate(name = as.numeric(name)) %>%
  plot(xlab = "Days", ylab = "Types", xlim = c(0, 7),
       ylim = c((min(.$val) %/% 10) * 10, (max(.$val) %/% 10 + 1) * 15))
abline(h=mean_Week, col='blue') 
abline(h= mean_Week_pos, col='green',lty=2) 
abline(h= mean_Week_neg, col='orange',lty=2)

}
scatter_date("2021-07-01",df)

Your code works for me.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(tidyr)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.0.5

df <- structure(
  list(Id=c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1),
       date1 = c("2021-07-20","2021-07-20","2021-07-20","2021-07-20","2021-07-20",
                 "2021-07-20","2021-07-20","2021-07-20","2021-07-20","2021-07-20","2021-07-20",
                 "2021-07-20","2021-07-20","2021-07-20","2021-07-20","2021-07-20","2021-07-20",
                 "2021-07-20","2021-07-20","2021-07-20","2021-07-20"),
       date2 = c("2021-07-01","2021-07-01","2021-07-01","2021-07-01","2021-04-02",
                 "2021-04-02","2021-04-02","2021-04-02","2021-04-02","2021-04-02","2021-04-03",
                 "2021-04-03","2021-04-03","2021-04-03","2021-04-03","2021-04-08","2021-04-08",
                 "2021-04-09","2021-04-09","2021-04-10","2021-04-10"),
       Week= c("Thursday","Thursday","Thursday","Thursday","Friday","Friday","Friday","Friday",
               "Friday","Friday","Saturday","Saturday","Saturday","Saturday","Saturday","Thursday",
               "Thursday","Friday","Friday","Saturday","Saturday"),
       D = c("","","Ho","","","","","","Ho","","","","","","","","","","","",""),
       D1 = c(8,1,9, 3,5,4,7,6,3,8,2,3,4,6,7,8,4,2,6,2,3), DR01 = c(4,1,4,3,3,4,3,6,3,7,2,3,4,6,7,8,4,2,6,7,3),
       DR2 = c(2,1,4,3,3,4,1,6,3,7,2,3,4,6,7,8,4,2,6,2,3), DR03 = c(7,5,4,3,3,4,1,5,3,3,2,3,4,6,7,8,4,2,6,4,3)),
  class = "data.frame", row.names = c(NA, -21L))

df<-subset(df,df$date2<df$date1)

dim_data<-dim(df)

day<-c(seq.Date(from = as.Date(df$date2[1]), by = "days",
                length = dim_data[1]
)) ###<--------- issue here

df_grouped <- df %>%
  mutate(across(starts_with("date"), as.Date)) %>%
  group_by(date2) %>%
  summarise(Id = first(Id),
            date1 = first(date1),
            Week = first(Week),
            D = first(D),
            D1 = sum(D1)) %>%
  select(Id,date1,date2,Week,D,D1)
#> `summarise()` ungrouping output (override with `.groups` argument)

df_grouped <- df_grouped %>% mutate(date1=format(date1,"%d/%m/%Y"),
                                    date2=format(date2,"%d/%m/%Y"))
df_grouped<-data.frame(df_grouped)

#create scatter plot
scatter_date <- function(dt, dta = df) {
  
  # get the week day
  my_day <- weekdays(as.Date(dt))
  
  df_OC<-subset(df_grouped,is.na(D) | D=="") ###<-----------  issue here
  ds_OC<-subset(df_OC,df_OC$Week==my_day) 
  
  
  mean_Week<-mean(as.numeric(ds_OC[,"D1"]) )
  sdeviation_Week<-sd(as.numeric(ds_OC[,"D1"]))
  
  
  mean_Week_pos <- (mean_Week + sdeviation_Week)
  mean_Week_neg <- (mean_Week - sdeviation_Week)
  
  dta %>%
    filter(date2 == ymd(dt)) %>%
    summarize(across(starts_with("DR"), sum)) %>%
    pivot_longer(everything(), names_pattern = "DR(.+)", values_to = "val") %>%
    mutate(name = as.numeric(name)) %>%
    plot(xlab = "Days", ylab = "Types", xlim = c(0, 7),
         ylim = c((min(.$val) %/% 10) * 10, (max(.$val) %/% 10 + 1) * 15))
  abline(h=mean_Week, col='blue') 
  abline(h= mean_Week_pos, col='green',lty=2) 
  abline(h= mean_Week_neg, col='orange',lty=2)
  
}
scatter_date("2021-07-01",df)

Created on 2021-08-27 by the reprex package (v0.3.0)

@FJCC , I figured it would work. What do you think might be happening? lack of update of any package? error of something?

is your R session capable of plotting an abline with color ?

plot(c(-2,3), c(-1,5), type = "n", xlab = "x", ylab = "y", asp = 1)
## the x- and y-axis, and an integer grid
abline(h = 0, v = 0, col = "red")

image

It worked normally!

plot(c(-2,3), c(-1,5), type = "n", xlab = "Test1", ylab = "Test2", asp = 1)
abline(h = 0, v = 0, col = "red")

@nirgrahamuk , I inserted below an image showing that when I load the lubridate and dplyr packages and there are some warnings. Could this be a possible error of not generating the lines?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.