Пытаюсь нарисовать график по заданию, проблема что его нужно copy 1 в 1, всё что нужно прилагается(ссылка на данные в коде)

Задание разработать код что фильтрует данные и строит по ним график.

В задании мы работали с набором данных о невыходах курьеров на работу. Считайте данные, записывайте дни как сезоны и удаляйте записи, соответствующие дисциплинарному отказу, равному 1. У вас должно остаться 700 строк. Сделайте этот график с помощью ggplot: введите сюда описание изображения

Вот мой код и некоторые данные что получились:

# Load the required packages
#install.packages("googlesheets4", repos = "http://cran.us.r-project.org")
#install.packages('tidyverse', repos = "http://cran.us.r-project.org")
#install.packages("ggplot2", repos = "http://cran.us.r-project.org")

library(googlesheets4)
library(tidyverse)
library(ggplot2)

# Authenticate with Google Sheets API
#gs4_auth() # Replace with the path to your credentials file

# Access the Google Sheet
data <- read_sheet('https://docs.google.com/spreadsheets/d/1nmf0ba1AFFLXMXWq_1nO8CeRqT8CZMhuFsDgu7BjKts/edit?usp=sharing')

# Recode seasons based on Month column
data <- data %>%
  mutate(Season = case_when(data$'Month of absence' %in% c(12, 1, 2) ~ "Winter",
                            data$'Month of absence' %in% c(3, 4, 5) ~ "Spring",
                            data$'Month of absence' %in% c(6, 7, 8) ~ "Summer",
                            data$'Month of absence' %in% c(9, 10, 11) ~ "Fall",
                            TRUE ~ NA_character_))

# Remove entries corresponding to disciplinary failure equal to 1
data <- data %>%
  filter(data$'Disciplinary failure' != 1)

df_reason_count <- data %>%
  group_by(ID, 'Reason for Absence') %>%
  summarize(count = n(), .groups = "drop")

print(df_reason_count)
#print(nrow(df_reason_count))
df_reason_count$ID <- as.factor(df_reason_count$ID)

#df_reason_count$hours <- as.factor(df_reason_count$count)
df_reason_count$hours <- reorder(df_reason_count$ID, df_reason_count$count)  # Reorder factor levels
print(df_reason_count)
ggplot(df_reason_count, aes(x = 'Reason for Absence', y = hours, fill = as.factor(ID))) +
  geom_col(position = "dodge") +
  labs(x = "Hours", y = " ") +
  ggtitle("Accumulated and Individual Absence Times") +
  scale_fill_discrete(name = "hours") +
  theme_bw()

После фильтрации осталось 700 записей, которые потом группируются по ID

И сам график: введите сюда описание изображения


Ответы (0 шт):