Personnaliser les préférences

Nous utilisons des cookies pour vous aider à naviguer efficacement et à exécuter certaines fonctions. Vous trouverez ci-dessous des informations détaillées sur tous les cookies sous chaque catégorie de consentement.

Les cookies classés comme « Nécessaires » sont stockés sur votre navigateur car ils sont essentiels pour activer les fonctionnalités de base du site.... 

Toujours actif

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

Aucun cookie à afficher.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

Aucun cookie à afficher.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

Aucun cookie à afficher.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

Aucun cookie à afficher.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

Aucun cookie à afficher.

Comment ajouter une étiquette à geom_vline dans ggplot2



Vous pouvez utiliser la syntaxe de base suivante pour ajouter une étiquette à une ligne verticale dans ggplot2 :

+ annotate("text", x=9, y=20, label="Here is my text", angle=90)

Les exemples suivants montrent comment utiliser cette syntaxe dans la pratique.

Exemple 1 : ajouter une étiquette à geom_vline

Le code suivant montre comment ajouter une étiquette à une ligne verticale dans ggplot2 :

library(ggplot2)

#create data frame 
df <- data.frame(x=c(1, 3, 3, 4, 5, 5, 6, 9, 12, 15),
                 y=c(13, 14, 14, 12, 17, 21, 22, 28, 30, 31))

#create scatterplot with vertical line at x=10
ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  geom_vline(xintercept=10) +
  annotate("text", x=9.7, y=20, label="Some text", angle=90)

Exemple 2 : ajouter une étiquette personnalisée à geom_vline

Le code suivant montre comment utiliser les arguments size et color pour ajouter une étiquette avec une taille et une couleur personnalisées à une ligne verticale dans ggplot2 :

library(ggplot2)

#create data frame 
df <- data.frame(x=c(1, 3, 3, 4, 5, 5, 6, 9, 12, 15),
                 y=c(13, 14, 14, 12, 17, 21, 22, 28, 30, 31))

#create scatterplot with vertical line at x=10
ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  geom_vline(xintercept=10) +
  annotate("text", x=9, y=20, label="Some text", angle=90, size=15, color="blue")

Exemple 3 : ajouter plusieurs étiquettes à geom_vline

Le code suivant montre comment utiliser la fonction annotate() plusieurs fois pour ajouter plusieurs étiquettes à une ligne verticale dans ggplot2 :

library(ggplot2)

#create data frame 
df <- data.frame(x=c(1, 3, 3, 4, 5, 5, 6, 9, 12, 15),
                 y=c(13, 14, 14, 12, 17, 21, 22, 28, 30, 31))

#create scatterplot with vertical line at x=10
ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  geom_vline(xintercept=10) +
  annotate("text", x=9, y=20, label="Some text", angle=90, size=15, color="blue") +
  annotate("text", x=11, y=20, label="More text", angle=90, size=13, color="red")

N’hésitez pas à utiliser la fonction annotate() autant de fois que vous le souhaitez pour ajouter autant d’étiquettes que vous le souhaitez au tracé.

Ressources additionnelles

Les didacticiels suivants expliquent comment effectuer d’autres tâches courantes dans ggplot2 :

Comment tracer une ligne de régression linéaire dans ggplot2
Comment définir les limites de l’axe dans ggplot2
Comment créer des tracés côte à côte dans ggplot2

Ajouter un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *