Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a bold female symbol to ggplot2 using annotate

I'm trying to use the female symbol, ♀ in my plot. It's pretty faint (well, it looks faint on my actual graph), so I was hoping to make it bold face.

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

Plot with faint female symbol

I've tried the following, but none seem to work:

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) 

I also found this post, but I'm not sure if I can modify the following code to work within ggplot?

plot(df)
text( locator(1), "\\VE", vfont=c("sans serif","bold"), xpd=TRUE)  # Venus 
like image 675
Nova Avatar asked Jul 13 '16 14:07

Nova


People also ask

How do I annotate a plot in R?

If you want to annotate your plot or figure with labels, there are two basic options: text() will allow you to add labels to the plot region, and mtext() will allow you to add labels to the margins. For the plot region, to add labels you need to specify the coordinates and the label.


1 Answers

@Axeman's comment helped me find an answer - I didn't realize you could load in other packages to get more fonts for ggplot2. Thanks! I used the following code:

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

like image 110
Nova Avatar answered Oct 06 '22 06:10

Nova