这是这个问题的后续(第 1 部分):ggplot (geom_point) 中的自定义形状
这提供了一个解决方案来生成头骨💀(或心❤)形式的自定义 ggplot2 形状:
library(ggplot2)
df <- read.table(text="x y
1 3
2 4
3 6
4 7", header=TRUE)
ggplot(data = df, aes(x =x, y=y)) +
geom_point(shape="\u2764",
colour = "red",
fill = "white",
size = 6)
创建于 2023-12-25,使用 reprex v2.0.2
非常好。 但是出现了一个问题,我指定了白色填充,但里面还是透明的。如何将其填充为白色?
使用
geom_text()
,您可以更好地控制使用的字体(例如,使用 Font Awesome 图标);使用 2 geom_text()
层,您可以首先绘制实心(白色)心形,然后添加常规字形以添加红色轮廓。如果可以选择使用 AGG 图形设备(ragg
包、RStudio 中的工具/全局选项/常规/图形 设置、用于 knit 的 ragg_png
设备),基于 systemfonts
的解决方案可能看起来像这样:
library(ggplot2)
library(systemfonts)
# otfs folder from FA6 free desktop download, https://fontawesome.com/download
fs::dir_tree("otfs")
#> otfs
#> ├── Font Awesome 6 Brands-Regular-400.otf
#> ├── Font Awesome 6 Free-Regular-400.otf
#> └── Font Awesome 6 Free-Solid-900.otf
# register Font Awesome regular and solid fonts from local path
register_font("fa-regular", "otfs/Font Awesome 6 Free-Regular-400.otf")
register_font("fa-solid", "otfs/Font Awesome 6 Free-Solid-900.otf")
df <- read.table(text="x y
1 3
2 4
3 6
4 7", header=TRUE)
ggplot(data = df, aes(x =x, y=y)) +
geom_text(label = "heart", family = "fa-solid", colour = "white", size = 6) +
geom_text(label = "heart", family = "fa-regular", colour = "red", size = 6)
创建于 2023-12-25,使用 reprex v2.0.2