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 créer un histogramme de deux variables dans R



Un histogramme est un moyen utile de visualiser la distribution des valeurs pour une variable donnée.

Pour créer un histogramme pour une variable dans R, vous pouvez utiliser la fonction hist() . Et pour créer un histogramme pour deux variables dans R, vous pouvez utiliser la syntaxe suivante :

hist(variable1, col='red')
hist(variable2, col='blue', add=TRUE)

L’exemple suivant montre comment utiliser cette syntaxe dans la pratique.

Exemple : créer un histogramme de deux variables dans R

Le code suivant montre comment créer un histogramme de deux variables dans R :

#make this example reproducible
set.seed(1)

#define data
x1 = rnorm(1000, mean=0.6, sd=0.1)
x2 = rnorm(1000, mean=0.4, sd=0.1)

#plot two histograms in same graph
hist(x1, col='red')
hist(x2, col='blue', add=TRUE)

Puisque les valeurs des histogrammes se chevauchent, c’est une bonne idée d’utiliser des couleurs rgb() avec une transparence accrue :

#make this example reproducible
set.seed(1)

#define data
x1 = rnorm(1000, mean=0.6, sd=0.1)
x2 = rnorm(1000, mean=0.4, sd=0.1)

#plot two histograms in same graph
hist(x1, col=rgb(0,0,1,0.2), xlim=c(0, 1),
     xlab='Values', ylab='Frequency', main='Histogram for two variables')
hist(x2, col=rgb(1,0,0,0.2), add=TRUE)

Histogramme pour deux variables dans R

Vous pouvez également ajouter une légende pour faciliter l’interprétation des histogrammes :

#make this example reproducible
set.seed(1)

#define data
x1 = rnorm(1000, mean=0.6, sd=0.1)
x2 = rnorm(1000, mean=0.4, sd=0.1)

#plot two histograms in same graph
hist(x1, col=rgb(0,0,1,0.2), xlim=c(0, 1),
     xlab='Values', ylab='Frequency', main='Histogram for two variables')
hist(x2, col=rgb(1,0,0,0.2), add=TRUE)

#add legend
legend('topright', c('Variable 1', 'Variable 2'),
       fill=c(rgb(0,0,1,0.2), rgb(1,0,0,0.2)))

Histogramme de deux variables dans l'exemple R


Vous pouvez trouver plus de didacticiels R sur cette page .

Ajouter un commentaire

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