ggplot2 annotate文本设置意大利斜体

有时ggplot作图时希望某些字变成斜体,可以导入Unicode包实现:

library(Unicode)
p.val <- ' < 1e-4'
italic_p <- u_char_inspect(u_char_from_name("MATHEMATICAL ITALIC SMALL P"))["Char"]

cairo_pdf('~/Downloads/tmp/test.pdf', height=5, width=8)
ggplot(data=controls, aes(x = control_num)) + geom_histogram(stat="count", fill='#888888', color='white') +
  labs(x='', title = 'ENCFF001HIA') +
  geom_vline(xintercept = 47, color='#FF5511') +
  geom_vline(xintercept = mean(controls$control_num), color='#0066FF') +
   theme_bw() + theme(plot.title = element_text(hjust=0.5)) +
  annotate("text", x = -Inf, y = Inf, label = paste(italic_p, p.val, sep=''), hjust = -.2, vjust = 2)
dev.off()

可以实现p为斜体:



注意保存的时候,要使用cairo_pdf()而不是pdf(),否则p有可能显示不出来。

参考:
1). https://www.codenong.com/30673470/
2). https://www.coder.work/article/6252692

你可能感兴趣的:(ggplot2 annotate文本设置意大利斜体)