【问题标题】:Add a bold female symbol to ggplot2 using annotate使用 annotate 向 ggplot2 添加一个粗体的女性符号
【发布时间】:2016-11-16 05:46:59
【问题描述】:

我正在尝试在我的情节中使用女性符号♀。它很微弱(嗯,在我的实际图表上看起来很微弱),所以我希望让它变得粗体。

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) 

我也找到了this post,但我不确定是否可以修改以下代码以在 ggplot 中工作?

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

【问题讨论】:

  • 您可以将fontface = 'bold' 添加到annotate 调用中,2016 会看起来很粗,但 在我的屏幕上看起来是一样的。我的猜测是该字体没有该字符的粗体表示?
  • 是的,如果你使用“2016~(bold(infinity))”,无穷大符号也不会加粗......
  • 如果其他familys 确实有这些大胆的面孔,你可以试试。

标签: r ggplot2 fonts plotmath


【解决方案1】:

@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")

【讨论】:

  • 嗨,你怎么知道family是annotate的参数? ?annotate 没有告诉我。
  • 嗨@AlvaroMorales,在帮助文件中,您将在参数列表中看到..., 参数。当您阅读文档时,它说“Other arguments passed on to layer(). These are often aesthetics, used to set an aesthetic to a fixed value, like colour = "red" or size = 3.”家庭论点只是另一种美学,所以它在这里有效。
猜你喜欢
  • 2019-08-24
  • 1970-01-01
  • 1970-01-01
  • 2022-11-04
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 2019-12-17
相关资源
最近更新 更多