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 utiliser un arrière-plan transparent dans ggplot2



Vous pouvez utiliser la syntaxe suivante pour créer un arrière-plan transparent dans un tracé dans ggplot2 :

p +
  theme(
    panel.background = element_rect(fill='transparent'), #transparent panel bg
    plot.background = element_rect(fill='transparent', color=NA), #transparent plot bg
    panel.grid.major = element_blank(), #remove major gridlines
    panel.grid.minor = element_blank(), #remove minor gridlines
    legend.background = element_rect(fill='transparent'), #transparent legend bg
    legend.box.background = element_rect(fill='transparent') #transparent legend panel
  )

Si vous décidez d’exporter le tracé à l’aide de ggsave() , veillez à spécifier que l’arrière-plan doit être transparent :

ggsave('myplot.png', p, bg='transparent')

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

Exemple : utiliser un arrière-plan transparent dans ggplot2

Le code suivant montre comment créer un simple boxplot groupé dans ggplot2 :

library(ggplot2) 

#make this example reproducible
set.seed(1)

#create dataset
data <- data.frame(team=rep(c('A', 'B', 'C'), each=50),
                   program=rep(c('low', 'high'), each=25),
                   values=seq(1:150)+sample(1:100, 150, replace=TRUE))

#create boxplot
ggplot(data, aes(x=team, y=values, fill=program)) + 
  geom_boxplot()

Nous pouvons utiliser le code suivant pour créer un arrière-plan transparent pour le tracé :

library(ggplot2) 

#make this example reproducible
set.seed(1)

#create dataset
data <- data.frame(team=rep(c('A', 'B', 'C'), each=50),
                   program=rep(c('low', 'high'), each=25),
                   values=seq(1:150)+sample(1:100, 150, replace=TRUE))

#create boxplot
p <- ggplot(data, aes(x=team, y=values, fill=program)) + 
       geom_boxplot() +
       theme(
         panel.background = element_rect(fill='transparent'),
         plot.background = element_rect(fill='transparent', color=NA),
         panel.grid.major = element_blank(),
         panel.grid.minor = element_blank(),
         legend.background = element_rect(fill='transparent'),
         legend.box.background = element_rect(fill='transparent')
       )

#display boxplot
p

On peut ensuite exporter ce tracé vers un fichier PNG, en précisant que le fond doit être transparent dans l’image exportée :

ggsave('grouped_boxplot.png', p, bg='transparent')

Si j’ouvre ce fichier exporté sur mon ordinateur, je constate que le fond est bien transparent :

Ressources additionnelles

Comment changer la taille de la police dans ggplot2
Comment supprimer une légende dans ggplot2
Comment supprimer le quadrillage dans ggplot2

Ajouter un commentaire

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