ggplot2 has four main components

  1. Theme elements specify the non-data elements that you can control
  2. Each element is associated with an element function, which describes the visual properties of the element
  3. The theme() function which allows you to override the default theme elements by calling element functions
  4. Complete themes

Theme elements

There are around 40 unique elements that control the appearance of the plot. They can be roughly grouped into five categories: plot, axis, legend, panel and facet.

We will actively modify the base graph as we progress

library(ggplot2)
df <- data.frame(x = 1:3, y = 1:3, z = LETTERS[1:3])
base <- ggplot(df, aes(x, y)) + geom_point(size = 4)

Plot background and panel

Both these elements are usually modifidied with setter function element_rect. Plot title and panel grid lines wqill be modified with element_text and element_line which I describe below when I discuss axes.

The syntax for element_rect looks like this:

element_rect(fill = NULL, colour = NULL, size = NULL, linetype = NULL,
  color = NULL, inherit.blank = FALSE)

The options color and size pertain to the border around the rectangle.

base + theme(plot.background = element_rect(fill = "lightblue"))

base + theme(panel.background = element_rect(fill = "lightblue"))

base + theme(plot.background = element_rect(fill = "pink")) +
  theme(panel.background = element_rect(fill = "lightblue"))

Let’s put a dark border around the plot

base + theme(plot.background = element_rect(fill = "pink", color = "black", size = 3)) +
  theme(panel.background = element_rect(fill = "lightblue"))

Change the linetype

base + theme(plot.background = element_rect(fill = "pink", color = "black", size = 3, linetype = "dashed")) +
  theme(panel.background = element_rect(fill = "lightblue"))

You can change the way the dashes appear

base + theme(plot.background = element_rect(fill = "pink", color = "black", size = 1, linetype = "2112")) +
  theme(panel.background = element_rect(fill = "lightblue"))

You can adjust the major and minor gridlines with element_line function

base + theme(panel.grid.major = element_line(color = "blue", size = 2, linetype = "dashed"))

base + theme(panel.grid.minor = element_line(color = "blue", size = 2, linetype = "dashed"))

For many professional applications it is important to set the aspect ratio of the panel so that the graph looks the same no matter where you print it. Flexible aspect ratio may lead to unnecessary stretching or compressing of axes.

base + theme(aspect.ratio = 1)

Axes

Axes have two commonly used setter functions: element_text and element_line

The first is used for modifying all sort of axis text including axis title and tick text. The latter is used for the axis and tick lines.

The synatx for the two functions is as follows:

element_text(family = NULL, face = NULL, colour = NULL, size = NULL,
  hjust = NULL, vjust = NULL, angle = NULL, lineheight = NULL,
  color = NULL, margin = NULL, debug = NULL, inherit.blank = FALSE)


element_line(colour = NULL, size = NULL, linetype = NULL,
  lineend = NULL, color = NULL, arrow = NULL, inherit.blank = FALSE)

# where arrow describes the arrow head with the function arrow() with the following syntax

arrow(angle = 30, length = unit(0.25, "inches"),
      ends = "last", type = "open")

The default ggplot2 doesn’t have a line with arrow on the X or Y axis. Let’s put it there.

base + theme(axis.line = element_line(arrow = arrow(angle = 15, length = unit(.15,"inches"),type = "closed")))

Modify axis ticks

base + theme(axis.ticks = element_line(color = "red"),
             axis.ticks.length = unit(10, units = "pt"),
             axis.text = element_text(face = "bold", family = "Josefin Slab", size = 12, angle = 30))

base + geom_point(aes(color = z), size = 4) +
  theme(legend.key = element_blank(),
        legend.background = element_blank(),
        legend.title.align = 1,
        legend.position = c(0.9,0.5))

Changing strips and panels in facet_wrap and facet_grid

We will agin use the mpg2 data set

mpg2 <- subset(mpg, cyl != 5 & drv %in% c("4", "f") & class != "2seater")
ggplot(mpg2, aes(displ,hwy)) +
  geom_point() +
  facet_wrap(~class) +
  theme(strip.background = element_blank()) +
  theme(strip.text = element_text(family = "Times")) +
  theme(panel.spacing.x = unit(1,"cm"), panel.spacing.y = unit(1.5,"cm"))

Get ggthemes and cowplot and play with the themes

Saving your plots

ggplot2 has its own inbuilt ggsave function that we will use

# vector graphics
ggsave("plot01_20170628.svg")
ggsave("plot01_20170628.pdf")

# raster graphics 
ggsave("plot01_20170628.png")