Skip to content

Instantly share code, notes, and snippets.

@andrewheiss
Created May 7, 2015 04:04
Show Gist options
  • Save andrewheiss/ac686b1a057003291e62 to your computer and use it in GitHub Desktop.
Save andrewheiss/ac686b1a057003291e62 to your computer and use it in GitHub Desktop.
Custom fonts in ggplot2 with Cairo
library(ggplot2)
library(Cairo) # MAGIC PACKAGE
fake.data <- data.frame(x = rnorm(100), y = rnorm(100), z = rbinom(100, 1, 0.5))
p <- ggplot(fake.data, aes(x=x, y=y)) +
geom_line() +
labs(x="Science", y="More science", title="Here's a title") +
facet_wrap(~ z) +
theme_bw() +
# Add whatever fonts you want to any element_text() things in theme()
theme(title=element_text(family="Source Sans Pro Semibold"),
axis.title=element_text(family="Source Sans Pro Light"),
strip.text=element_text(family="Source Sans Pro Light"))
p
# device=cairo_pdf is the magic part. That's how the fonts get embedded.
ggsave(p, filename="~/Desktop/blah.pdf",
width=5, height=3.5, units="in", device=cairo_pdf)
ggsave(p, filename="~/Desktop/blah.png",
width=5, height=3.5, units="in")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment