install.packages("hexSticker")
I am going to demonstrate on how to create hexagon sticker using R. Thanks to GuangChuang Yu for creating the hexSticker
package. I am goint to use the hexSticker
package to create a hexagon sticker with the following code step-by-step:
First, install the hexSticker
package from CRAN:
Then, load the hexSticker
package:
library(hexSticker)
Next, hex sticker can be created using different approaches.
using packages such as
ggplot2
,lattice
using image file
ggplot2
library(ggplot2)
<- ggplot(aes(x = wt, y = mpg), data = mtcars) +
p geom_point() +
theme_minimal() +
theme(legend.position = "none")
<- p + theme_void() + theme_transparent()
p
<- sticker(p, package = "hexSticker", p_size = 10, s_x = 1, s_y = 1, filename = "sticker.png")
s
plot(s)
sticker()
function is used to create the sticker.
p
is the ggplot objectpackage
is the package name.p_size
is the size of the sticker.s_x
ands_y
are the x and y position of the sticker.filename
is the name of the file to save the sticker.plot()
function is used to preview the sticker.
Image File
<- system.file("figures/cat.png", package = "hexSticker")
image_url <- sticker(image_url, package = "hexSticker", p_size = 20, s_x = 1, s_y = 0.75, filename = "sticker2.png")
s2 plot(s2)
<- "/Users/joan/Documents/Joan Website/Joan Quarto/Rposts/2024-04-20-create-sticker/joanicon.png"
img
<- sticker(img, package = "Joan",
s3 p_family = "wqy-microhei", p_size = 20, p_x = 1, p_y = 1.65, s_x = 1.0, s_y = .85,
s_width = 0.5, s_height = 0.5, p_color = "blue", p_fill = "pink",
h_fill = "pink", h_color = "red",
filename = "s3.png")
Warning in ggplot2::annotate("text", x = x, y = y, size = size, label =
package, : Ignoring unknown parameters: `p_fill`
plot(s3)
image_url
is the path of the image file.
system.file()
function is used to get the path of the image file from a package.
p_color
is the color of the package namep_fill
is the fill color of the package nameh_fill
is the fill color of the hexagonh_color
is the color of the hexagon border
Happy creating sticker~
Citation
BibTeX citation:
@online{tan2024,
author = {Tan, Joan},
title = {Create {Hex} {Sticker} in {R}},
date = {2024-04-20},
url = {https://joantan.org/R/2024-04-20-create-sticker/},
langid = {en}
}
For attribution, please cite this work as:
Tan, Joan. 2024. “Create Hex Sticker in R.” April 20, 2024.
https://joantan.org/R/2024-04-20-create-sticker/.