在 R 中调整图中的线

Adjust line in graph in R

下图根据date2生成散点图。此外,还会生成一条表示平均值的水平线。如您所见,一周中的每一天都有不同的平均值。

请注意,我在 abline 中指定了 h=mean_saturday,因为 10/4 是星期六。但我不想总是必须更改 abline 的这一部分以显示正确的平均线,但我的想法是自动保留它,也就是说,当我在代码中输入日期 10/4/2021 时,代码已经识别出 10 号是星期六并插入适当的均值线。知道怎么做吗?

library(dplyr)
library(ggplot2)
library(tidyr)
library(lubridate)
library(tibble)
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","","","","","","","","","","","",""), 
       DR01 = c(2,1,4,3,3,4,1,6,3,7,2,3,4,6,7,8,4,2,6,2,3)),
  class = "data.frame", row.names = c(NA, -21L))

mean_thursday=4
mean_friday=5
mean_saturday=6


scatter_date <- function(dt, dta = df) {
  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_saturday, col='blue') 
}  
scatter_date("2021-04-10",df)

您可以尝试将 scatter_date 函数中的输入日期转换为日期并获取工作日:my_day <- weekdays(as.Date(dt)) 将其添加到您的 switch 语句中:

my_mean <- switch(
  my_day,
  "Saturday" = mean_saturday,
  "Friday" = mean_friday,
  "Thursday" = mean_thursday,
  0) # add here your other days

并将abline(h=mean_saturday, col='blue')中的mean_saturday替换为my_mean

这里是完整代码:

library(dplyr)
library(ggplot2)
library(tidyr)
library(lubridate)
library(tibble)
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","","","","","","","","","","","",""), 
       DR01 = c(2,1,4,3,3,4,1,6,3,7,2,3,4,6,7,8,4,2,6,2,3)),
  class = "data.frame", row.names = c(NA, -21L))

mean_thursday=4
mean_friday=5
mean_saturday=6


scatter_date <- function(dt, dta = df) {
  
  my_day <- weekdays(as.Date(dt))
  
  my_mean <- switch(
    my_day,
    "Saturday" = mean_saturday,
    "Friday" = mean_friday,
    "Thursday" = mean_thursday,
    0) # add here your other days
  
  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), main = paste0(my_day, ":", my_mean),
         ylim = c((min(.$val) %/% 10) * 10, (max(.$val) %/% 10 + 1) * 15))
  abline(h=my_mean, col='blue') 
}  
# testing the different means
scatter_date("2021-04-10",df)
scatter_date("2021-04-9",df)
scatter_date("2021-04-8",df)

一种方法是定义包含感兴趣天数的平均值的 data.frame,然后使用 weekdays 从 table 中提取相应的平均值。

而不是

mean_thursday=4
mean_friday=5
mean_saturday=6

我会选择这样的东西:

mean_df <- data.frame(mean = c(4:6),
                      day = c('Thursday', 'Friday', 'Saturday'))

然后

abline(h=subset(mean_df, day == weekdays(as.Date(dt)))$mean, col='blue')

这将是您函数中的唯一变化。