Comment Créer une Icone dans R



Comment Créer une Icone dans R

Cet article décrit comment créer une icône dans R avec un fond transparent à partir d’un ggplot.

 

Prérequis

Charger ggplot2 et créer un thème d’aide pour l’icône ggplot:

library(ggplot2)

# Thème d'aide pour l'icône ggplot
theme_icon <- function () {
    theme_void() + 
    theme(
      panel.background = element_rect(fill = "transparent", colour = NA), 
      plot.background = element_rect(fill = "transparent", colour = NA), 
      legend.background = element_rect(fill = "transparent", colour = NA), 
      legend.box.background = element_rect(fill = "transparent", colour = NA)
    )
}

Créer une icône

  • Créer une icône à partir d’un graphique ggplot
p <- ggplot(iris, aes(Species, Sepal.Length)) + 
  geom_boxplot(color = "#478bca", fill = "transparent") +
  theme_icon()
  • Enregistrer l’icône au format png ou svg 72x72 pixels:
# SVG
ggsave(
  filename = "figures/boxplot-icon_72px.svg", p, 
  dpi=72, width = 1, height = 1
  )

# PNG
ggsave(
  filename = "figures/boxplot-icon_72px.png", p, 
  dpi=72, width = 1, height = 1, bg = "transparent"
  )

Créer une icône avec un autocollant hexagonal

Nous utiliserons le package R hexSticker. imageMagick est nécessaire pour installer hexSticker. Si vous ne l’avez pas installé, essayez les approches suivantes.

library(hexSticker)
p <- ggplot(iris, aes(Species, Sepal.Length)) + 
  geom_boxplot(color = "white", fill = "transparent") +
  theme_icon()
p.sticker <- sticker(
  p, package=" ", p_size=3, 
  s_x=1, s_y=1.1, s_width=1.3, s_height=1.5,
  h_color = "#478bca", h_fill = "#478bca",
  filename="figures/boxplot-icon-sticker.png"
  )
p.sticker



Version: English





No Comments

Give a comment

Want to post an issue with R? If yes, please make sure you have read this: How to Include Reproducible R Script Examples in Datanovia Comments