Home » R » ggplot2 » ggplot2 cheatsheet

ggplot2 cheatsheet

These are some of the most common things I want to do with ggplot2, but always forget the syntax (not helped when things are still evolving with each release!)

Titles, axis labels

  • main title
+ ggtitle("...")
  • set axis title (but see scale_x_continuous below)
+ xlab("...")
  • remove axis title, labels and tick marks (respectively)
+ theme(axis.title.x=element_blank(), 
        axis.text.x=element_blank(), 
        axis.ticks=element_blank())
  • make axis labels at a 45 degree angle
+ theme(axis.text.x = element_text(angle = 45, hjust = 1))
  • customize axis breaks and labels. title is optional, labels are optional and default to as.character(breaks)
scale_x_continuous("x axis title (optional)",
                   breaks=c(2000,5000),
                   labels=c("foo","bar"))
  • stretch axis to full width/length of plot
scale_x_continuous(expand=c(0,0))

Legend

  • no legend
+ theme(legend.position="none")

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s