Skip to content

Instantly share code, notes, and snippets.

@jdmar3
Forked from andrewheiss/ggplot_cairo.R
Last active August 29, 2015 14:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdmar3/c0c868948fa77d582188 to your computer and use it in GitHub Desktop.
Save jdmar3/c0c868948fa77d582188 to your computer and use it in GitHub Desktop.
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