Create Icon in R
This article describes how to create icon in R with transparent background from a ggplot.
Contents:
Prerequisites
Load ggplot2 and create a helper theme for ggplot icon:
library(ggplot2)
# Helper theme for ggplot icon
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)
)
}
Create an icon
- Create an icon form a ggplot graphic
p <- ggplot(iris, aes(Species, Sepal.Length)) +
geom_boxplot(color = "#478bca", fill = "transparent") +
theme_icon()
- Save the icon into a 72x72 pixel png or svg format:
# 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"
)
Create an icon with hexagon sticker
We’ll use the hexSticker R package. imageMagick
is required for installing hexSticker. If you have not installed it, please try the following approaches.
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
Recommended for you
This section contains best data science and self-development resources to help you on your path.
Books - Data Science
Our Books
- Practical Guide to Cluster Analysis in R by A. Kassambara (Datanovia)
- Practical Guide To Principal Component Methods in R by A. Kassambara (Datanovia)
- Machine Learning Essentials: Practical Guide in R by A. Kassambara (Datanovia)
- R Graphics Essentials for Great Data Visualization by A. Kassambara (Datanovia)
- GGPlot2 Essentials for Great Data Visualization in R by A. Kassambara (Datanovia)
- Network Analysis and Visualization in R by A. Kassambara (Datanovia)
- Practical Statistics in R for Comparing Groups: Numerical Variables by A. Kassambara (Datanovia)
- Inter-Rater Reliability Essentials: Practical Guide in R by A. Kassambara (Datanovia)
Others
- R for Data Science: Import, Tidy, Transform, Visualize, and Model Data by Hadley Wickham & Garrett Grolemund
- Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems by Aurelien Géron
- Practical Statistics for Data Scientists: 50 Essential Concepts by Peter Bruce & Andrew Bruce
- Hands-On Programming with R: Write Your Own Functions And Simulations by Garrett Grolemund & Hadley Wickham
- An Introduction to Statistical Learning: with Applications in R by Gareth James et al.
- Deep Learning with R by François Chollet & J.J. Allaire
- Deep Learning with Python by François Chollet
Version: Français
No Comments