Graphique - Heure et Minute

Postez ici vos questions, réponses, commentaires ou suggestions - Les sujets seront ultérieurement répartis dans les archives par les modérateurs

Modérateur : Groupe des modérateurs

Kévin Dubois
Messages : 97
Enregistré le : 25 Mar 2019, 12:07

Graphique - Heure et Minute

Messagepar Kévin Dubois » 22 Juin 2019, 17:46

Bonjour à tous,

Je dispose d'un fichier de donnée représentant un relevé naturaliste, qui comprend un premier champ en "Heure-Minute", et un deuxième champ d'entier qui correspond au relevé effectué (chiffre entre 0 et 100).

Disponible ici :
https://drive.google.com/open?id=1wKfrL ... uhy0BiiGiK

J'aimerais créer un graphique mettant en évidence ce deuxième champ suivant les date. En gros, en X je voudrais les heures et minutes, et en Y les relevés.

Je précise que le champ "Heure-Minute" a été crée grâce au package Lubridate sur R, et est de type période :

Code : Tout sélectionner

> class(releve$HEURE)
[1] "Period"
attr(,"package")
[1] "lubridate"


Merci à vous.

Mickael Canouil
Messages : 1315
Enregistré le : 04 Avr 2011, 08:53
Contact :

Re: Graphique - Heure et Minute

Messagepar Mickael Canouil » 24 Juin 2019, 11:44

Bonjour,

ggplot2 ne permet pas de gérer des objets de classes "Duration" ou "Period", ce qui se rapproche le plus serait date ou datetime (POSIX).

Code : Tout sélectionner

set.seed(20190624)
library(tidyverse)

fake_data <- expand.grid(sprintf("%02d", 0:23), sprintf("%02d", 0:59)) %>%
  tidyr::unite(col = "x", sep = ":") %>%
  dplyr::mutate(y = stats::rnorm(dplyr::n())) %>%
  dplyr::sample_frac(0.10)

ggplot2::ggplot(data = fake_data, mapping = ggplot2::aes(x = x, y = y)) +
  ggplot2::geom_line() +
  ggplot2::geom_point()
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?


Image

Code : Tout sélectionner

fake_data[["time"]] <- lubridate::hm(fake_data[["x"]])
ggplot2::ggplot(data = fake_data, mapping = ggplot2::aes(x = time, y = y)) +
  ggplot2::geom_line() +
  ggplot2::geom_point()
#> Warning: Removed 144 rows containing missing values (geom_path).
#> Warning: Removed 144 rows containing missing values (geom_point).


Image

Code : Tout sélectionner

fake_data[["time_asdate"]] <- lubridate::ymd_hm(paste(lubridate::today(), fake_data[["x"]])) # use the current date to have the right date format
ggplot2::ggplot(data = fake_data, mapping = ggplot2::aes(x = time_asdate, y = y)) +
  ggplot2::geom_line() +
  ggplot2::geom_point() +
  ggplot2::scale_x_datetime(labels = lubridate::hour) # to display only hour from the "time_asdate" variable (otherwise it is the "full" date)


Image

Cordialement,
Mickaël
mickael.canouil.fr | rlille.fr

Gabriel Terraz
Messages : 591
Enregistré le : 26 Sep 2011, 15:11

Re: Graphique - Heure et Minute

Messagepar Gabriel Terraz » 26 Juin 2019, 19:17

Bonjour,

Pourquoi ne pas faire quelque chose de plus simple comme ceci :

En partant directement de ton fichier :

Code : Tout sélectionner

tab <- read.table("clipboard", h = T, sep = ";", stringsAsFactors = F)
tab$time <- strptime(tab$HEURE, format = "%HH %MM 0S")


Ce qui donne ça comme objet :

Code : Tout sélectionner

tab <- structure(list(HEURE = c("4H 21M 0S", "14H 13M 0S", "14H 58M 0S",
"15H 35M 0S", "19H 19M 0S", "21H 41M 0S"), Releve = c(44L, 52L,
87L, 27L, 74L, 18L), time = structure(list(sec = c(0, 0, 0, 0,
0, 0), min = c(21L, 13L, 58L, 35L, 19L, 41L), hour = c(4L, 14L,
14L, 15L, 19L, 21L), mday = c(26L, 26L, 26L, 26L, 26L, 26L),
    mon = c(5L, 5L, 5L, 5L, 5L, 5L), year = c(119L, 119L, 119L,
    119L, 119L, 119L), wday = c(3L, 3L, 3L, 3L, 3L, 3L), yday = c(176L,
    176L, 176L, 176L, 176L, 176L), isdst = c(1L, 1L, 1L, 1L,
    1L, 1L), zone = c("CEST", "CEST", "CEST", "CEST", "CEST",
    "CEST"), gmtoff = c(NA_integer_, NA_integer_, NA_integer_,
    NA_integer_, NA_integer_, NA_integer_)), .Names = c("sec",
"min", "hour", "mday", "mon", "year", "wday", "yday", "isdst",
"zone", "gmtoff"), class = c("POSIXlt", "POSIXt"))), .Names = c("HEURE",
"Releve", "time"), row.names = c(2L, 4L, 1L, 5L, 3L, 6L), class = "data.frame")


Le graphe :

Code : Tout sélectionner

plot(tab$time, tab$Releve)


Si tu veux relier les points, un petit tri préalable :

Code : Tout sélectionner

tab <- tab[order(tab$time),]
plot(tab$time, tab$Releve, type = "b")


Comme cela me parait très simple, je suis peut être passé à coté de quelque chose

Kévin Dubois
Messages : 97
Enregistré le : 25 Mar 2019, 12:07

Re: Graphique - Heure et Minute

Messagepar Kévin Dubois » 10 Juil 2019, 10:21

En soit, je voudrais simplement afficher la répartition du score par heure, peut importe le jour.

Je vais tester ces méthode, un grand merci à vous.


Retourner vers « Questions en cours »

Qui est en ligne

Utilisateurs parcourant ce forum : Aucun utilisateur enregistré et 1 invité