Insert line in gerenerate graph

I would like help to insert the horizontal line into my graph below. Note that the abline function uses the variable m. I'll explain with examples because I believe it's easier to understand

Example: I defined in the dmda as 02/07 (Friday) and Code as CDE, so the variable m for this Data and Code is 4. Therefore, the horizontal line on the graph would have to be 4 for this day and the code I mentioned.

For 03/07 (Saturday), Code FGH, variable m is 5, then the horizontal line on the graph would have to be 5 for this date and code.

Therefore, the code below only generates the graph, but does not insert the line corresponding to the chosen date and code.

library(dplyr)
library(tidyverse)
library(lubridate)
library(stringr)

df1 <- structure(
  list(date1= c("2021-06-28","2021-06-28","2021-06-28","2021-06-28","2021-06-28",
                "2021-06-28","2021-06-28","2021-06-28"),
       date2 = c("2021-06-30","2021-06-30","2021-07-01","2021-07-01","2021-07-02","2021-07-02","2021-07-02","2021-07-03"),
       Code = c("ABC","BCD","ABC","CDE","DCE","CDE","FGH","FGH"),
       Week= c("Wednesday","Wednesday","Thursday","Thursday","Friday","Friday","Friday","Saturday"),
       DR1 = c(4,1,4,3,3,4,3,5),
       DR01 = c(4,1,4,3,3,4,3,6), DR02= c(4,2,6,7,3,2,7,4),DR03= c(9,5,4,3,3,2,1,5),
       DR04 = c(5,4,3,3,6,2,1,9),DR05 = c(5,4,5,3,6,2,1,9),
       DR06 = c(2,4,3,3,5,6,7,8),DR07 = c(2,5,4,4,9,4,7,8),
       DR08 = c(0,0,0,1,2,0,0,0),DR09 = c(0,0,0,0,0,0,0,0),DR010 = c(0,0,0,0,0,0,0,0),DR011 = c(4,0,0,0,0,0,0,0), 
       DR012 = c(0,0,0,3,0,0,0,5),DR013 = c(0,0,1,0,0,0,2,0),DR014 = c(0,0,0,0,0,2,0,0)),
  class = "data.frame", row.names = c(NA, -8L))

datas <- structure(
  list(Code = c("ABC","ABC","ABC","CDE","CDE","CDE","ABC","FGH","FGH"),
       Days = c(11,12,13,11,12,13,11,12,13),
       Numbers = c(11,17,13,12,12,12,17,12,15)),
  class = "data.frame", row.names = c(NA, -9L))

m<-df1 %>%
  group_by(Code,Week) %>%
  summarize(across(starts_with("DR1"), mean))
    > m
# A tibble: 8 x 3
# Groups:   Code [5]
  Code  Week        DR1
  <chr> <chr>     <dbl>
1 ABC   Thursday      4
2 ABC   Wednesday     4
3 BCD   Wednesday     1
4 CDE   Friday        4
5 CDE   Thursday      3
6 DCE   Friday        3
7 FGH   Friday        3
8 FGH   Saturday      5

dmda<-"2021-07-02"
CodeChosse<-"CDE"

f1 <- function(dat, code_nm) {
  dat <- subset(dat,  Code == code_nm)
  plot(Numbers ~ Days,  xlim= c(0,45), ylim= c(0,30),
       xaxs='i',data = dat,main = paste0(dmda, "-", code_nm))
   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)

Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) : 
  'list' object cannot be coerced to type 'double

enter image description here

m is a data frame, not a variable.

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.