使用注释向 ggplot2 添加粗体女性符号

7
我试图在我的绘图中使用女性符号,♀。它看起来很淡(至少在我的实际图表上是这样),所以我希望能够将其加粗。
df <- data.frame(x = c(0, 1), y = c(0, 1))
ggplot(df, aes(x, y)) + geom_point() +
   theme_bw() +
   annotate("text", x = 0.5, y = 0.7, label = "2016 ♀", 
   size = 7, hjust = 0, colour = "grey50")

带淡淡女性符号的图表

我尝试了以下方法,但似乎都不起作用:

ggplot(df, aes(x, y)) + geom_point() +
   annotate("text", x = 0.5, y = 0.7, label = "2016~bold(♀)", 
   size = 7, hjust = 0, parse = TRUE) 

# error message: Error in parse(text = as.character(lab)) : <text>:1:11: unexpected '<'
#1: 2016~bold(<
              ^

ggplot(df, aes(x, y)) + geom_point() +
   annotate("text", x = 0.5, y = 0.7, label = "2016~bold(u2640)", 
   size = 7, hjust = 0, parse = TRUE) 

ggplot(df, aes(x, y)) + geom_point() +
   annotate("text", x = 0.5, y = 0.7, label = "2016~bold(\u2640)", 
   size = 7, hjust = 0, parse = TRUE) 

我还发现了这篇帖子,但我不确定是否能修改以下代码以在ggplot中使用?

plot(df)
text( locator(1), "\\VE", vfont=c("sans serif","bold"), xpd=TRUE)  # Venus 

1
你可以在 annotate 调用中添加 fontface = 'bold',然后 2016 将会加粗显示,但是 在我的屏幕上看起来一样。我猜测这个字体可能没有该字符的加粗表示? - Axeman
是的,如果你使用“2016~(bold(infinity))”,无穷大符号也不会被加粗... - Nova
1
你可以尝试看看其他“family”是否有这些字体的粗体。 - Axeman
1个回答

8

@Axeman的评论帮助我找到了答案 - 我没有意识到你可以加载其他软件包以获取更多的ggplot2字体。谢谢!我使用了以下代码:

install.packages("extrafont")
library(extrafont)
font_import() # Prepare for this to take several minutes
loadfonts(device = "win")

ggplot(df, aes(x, y)) + geom_point() +
   theme_bw() +
   annotate("text", x = 0.5, y = 0.7, label = "2016 ♀", 
   size = 7, hjust = 0, colour = "grey50", family = "Calibri", fontface = "bold")

enter image description here


你好,你是怎么知道family是annotate的一个参数的呢?annotate并没有告诉我这一点。 - Alvaro Morales
嗨@AlvaroMorales,在帮助文件中,您将在参数列表中看到...,参数。当您阅读文档时,它说:“传递给layer()的其他参数。这些通常是美学,用于将美学设置为固定值,例如color =“red”或size = 3。” family参数只是另一个美学,因此在这里起作用。 - Nova

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接