Importation des librairies

library("ggplot2")
library("lubridate")
library("dplyr")

Exercice 1

df1 <- data.frame(
  Date = as.Date(c("2023-01-01", "2023-01-02", "2023-01-03", 
                   "2023-01-04", "2023-01-05", "2023-01-06", 
                   "2023-01-07", "2023-01-08", "2023-01-09", 
                   "2023-01-10")),
  Price = c(100, 105, 110, 108, 112, 115, 118, 120, 122, 125)
)

df1
##          Date Price
## 1  2023-01-01   100
## 2  2023-01-02   105
## 3  2023-01-03   110
## 4  2023-01-04   108
## 5  2023-01-05   112
## 6  2023-01-06   115
## 7  2023-01-07   118
## 8  2023-01-08   120
## 9  2023-01-09   122
## 10 2023-01-10   125

Exercice 2

dates <- seq(as.Date("2016-01-02"), by = "month", length.out = 10)
df2 <- data.frame(
  id = 1:10,
  date = dates,
  année = format(dates, "%Y"),
  mois = format(dates, "%m"),
  semaine = isoweek(dates),
  jour = format(dates, "%d")
)

df2
##    id       date année mois semaine jour
## 1   1 2016-01-02  2016   01      53   02
## 2   2 2016-02-02  2016   02       5   02
## 3   3 2016-03-02  2016   03       9   02
## 4   4 2016-04-02  2016   04      13   02
## 5   5 2016-05-02  2016   05      18   02
## 6   6 2016-06-02  2016   06      22   02
## 7   7 2016-07-02  2016   07      26   02
## 8   8 2016-08-02  2016   08      31   02
## 9   9 2016-09-02  2016   09      35   02
## 10 10 2016-10-02  2016   10      39   02

Exercice 3

df3 <- data.frame(
  id = 1:7,
  date = seq(
    from = as.POSIXct("2025-01-23 15:00", format = "%Y-%m-%d %H:%M"),
    to = as.POSIXct("2025-01-23 21:00", format = "%Y-%m-%d %H:%M"),
    by = "hour"
  )
)
df3$minute <- format(df3$date, "%M")
df3$seconde <- format(df3$date, "%S")

df3
##   id                date minute seconde
## 1  1 2025-01-23 15:00:00     00      00
## 2  2 2025-01-23 16:00:00     00      00
## 3  3 2025-01-23 17:00:00     00      00
## 4  4 2025-01-23 18:00:00     00      00
## 5  5 2025-01-23 19:00:00     00      00
## 6  6 2025-01-23 20:00:00     00      00
## 7  7 2025-01-23 21:00:00     00      00

Exercice 4

aujourdhui <- Sys.Date()
df2$jours_ecoules <- as.numeric(aujourdhui - df2$date)
df2$semaines_ecoulees <- round(df2$jours_ecoules / 7, 1)

intervalle <- interval(df2$date, aujourdhui)
df2$mois_ecoules <- round(time_length(intervalle, "month"), 1)
df2$annees_ecoulees <- round(time_length(intervalle, "year"), 1)

df2
##    id       date année mois semaine jour jours_ecoules semaines_ecoulees
## 1   1 2016-01-02  2016   01      53   02          3320             474.3
## 2   2 2016-02-02  2016   02       5   02          3289             469.9
## 3   3 2016-03-02  2016   03       9   02          3260             465.7
## 4   4 2016-04-02  2016   04      13   02          3229             461.3
## 5   5 2016-05-02  2016   05      18   02          3199             457.0
## 6   6 2016-06-02  2016   06      22   02          3168             452.6
## 7   7 2016-07-02  2016   07      26   02          3138             448.3
## 8   8 2016-08-02  2016   08      31   02          3107             443.9
## 9   9 2016-09-02  2016   09      35   02          3076             439.4
## 10 10 2016-10-02  2016   10      39   02          3046             435.1
##    mois_ecoules annees_ecoulees
## 1           109             9.1
## 2           108             9.0
## 3           107             8.9
## 4           106             8.8
## 5           105             8.8
## 6           104             8.7
## 7           103             8.6
## 8           102             8.5
## 9           101             8.4
## 10          100             8.3

Exercice 5

Série temporelle

ggplot(df1, aes(x = Date, y = Price)) +
  geom_line(color = "blue") +
  geom_point() +
  labs(title = "Évolution des prix", x = "Date", y = "Prix")

Graphique en chandelier

candle_data <- data.frame(
  Date = seq(as.Date("2023-01-01"), by = "day", length.out = 10),
  Open = c(100, 105, 110, 108, 112, 115, 118, 120, 122, 125),
  High = c(102, 107, 112, 110, 115, 118, 120, 122, 125, 128),
  Low = c(98, 103, 108, 106, 110, 113, 116, 118, 120, 123),
  Close = c(101, 106, 111, 109, 114, 117, 119, 121, 124, 127)
)

ggplot(candle_data, aes(x = Date)) +
  geom_segment(aes(x = Date, xend = Date, y = Low, yend = High), color = "black") +
  geom_rect(aes(xmin = Date - 0.2, xmax = Date + 0.2, ymin = pmin(Open, Close), ymax = pmax(Open, Close), fill = Open < Close)) +
  scale_fill_manual(values = c("TRUE" = "darkgreen", "FALSE" = "firebrick")) +
  labs(title = "Graphique en chandelier", x = "Date", y = "Prix", fill = "Tendance") +
  theme_minimal() +
  theme(legend.position = "none")

Visualisation de saisonnalité

season_data <- data.frame(
  Date = seq(as.Date("2020-01-01"), by = "month", length.out = 48),
  Ventes = round(100 + 50*sin(seq(0, 4*pi, length.out=48)) + rnorm(48, 0, 10))
)

ggplot(season_data, aes(x = Date, y = Ventes)) +
  geom_line(color = "steelblue") +
  geom_point() +
  geom_smooth(method = "loess", formula = y ~ x, color = "red", se = FALSE) +
  labs(title = "Saisonnalité des ventes", subtitle = "Avec tendance polynomiale") +
  scale_x_date(date_breaks = "3 months", date_labels = "%b %Y") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

Calendrier thermique

heatmap_data <- data.frame(
  Date = seq(as.Date("2023-01-01"), as.Date("2023-12-31"), by = "day"),
  Valeur = sample(10:100, 365, replace = TRUE)
) |>
  mutate(
    Mois = month(Date, label = TRUE, abbr = FALSE),
    Semaine = week(Date),
    Jour = wday(Date, label = TRUE)
  )

ggplot(heatmap_data, aes(Semaine, Jour, fill = Valeur)) +
  geom_tile(color = "pink") +
  facet_grid(.~Mois) +
  scale_fill_gradient(low = "pink", high = "purple") +
  labs(title = "Calendrier thermique 2023") +
  theme_minimal() +
  theme(axis.text.x = element_blank())