15 April 2019

Making Professional Plots

Making Professional Plots

Two main points:

  1. Tweaking the look of the previous plots
  2. Plot Resolution and Font Sizes

The package ggplot2

themes

ggplot2 uses themes to control the overall appearance

ggplot(transport, aes(x = gender, y = height, fill = gender)) +
  geom_boxplot() +
  theme_bw()

My default is theme_bw()

  • Removes the background grey, prints labels in black etc.

The package ggplot2

themes

ggplot(transport, aes(x = gender, y = height, fill = gender)) +
  geom_boxplot() +
  theme_classic()
ggplot(transport, aes(x = gender, y = height, fill = gender)) +
  geom_boxplot() +
  theme_void()

Setting a default theme

You can set a default theme for a workspace or session

theme_set(theme_bw())

Now all plots in the workspace will use theme_bw()

The package ggplot2

themes

The theme() function is also where you set:

  • axis.text, legend attributes etc.
  • Often uses elements to set an attribute
?theme

The package ggplot2

themes

Changing text within themes uses element_text()

?element_text
ggplot(transport, aes(x = gender, y = height, fill = gender)) +
  geom_boxplot() +
  theme_bw() +
  theme(axis.text.x = element_text(
      angle = 90, hjust = 1, vjust = 0.5
  ))

The package ggplot2

themes

Changing backgrounds and outlines uses element_rect()

ggplot(transport, aes(x = gender, y = height, fill = gender)) +
  geom_boxplot() +
  theme_bw() +
  theme(legend.background = element_rect(
      fill = "yellow", colour = "black"
  ))

The package ggplot2

themes

To remove all attributes use element_blank() in place of element_rect(), element_text() or element_line()

ggplot(transport, aes(x = gender, y = height, fill = gender)) +
  geom_boxplot() +
  theme_bw() +
  theme(panel.grid = element_blank())

Legends

We can move the legend to multiple places:

ggplot(transport, aes(x = gender, y = height, fill = gender)) +
  geom_boxplot() +
  theme_bw() +
  theme(legend.position = "bottom")

Legends

Or we can use co-ordinates to place it inside the plotting region

ggplot(transport, aes(x = gender, y = height, fill = gender)) +
  geom_boxplot() +
  theme_bw() +
  theme(legend.position = c(0.85, 0.15))

The plotting region is assumed to have width and height of 1

Other plot attributes

Axes

We can also edit axes, fills, outlines etc. using scale_...() layers

ggplot(transport, aes(x = gender, y = height, fill = gender)) +
  geom_boxplot() +
  theme_bw() +
  scale_y_log10(limits = c(100, 200), 
                breaks = c(100, 125, 150, 175)) 

Other plot attributes

Axes

We can turn off or modify plot expansion

ggplot(transport, aes(x = gender, y = height, fill = gender)) +
  geom_boxplot() +
  theme_bw() +
  scale_y_continuous(expand = c(0, 0)) 
  • The first a = 0 multiplies the scale by 1 + a
  • The second b = 0 adds \(\pm\)b to the axis extrema

Other plot attributes

Axes

Alternatively, we can use expand_scale() to set expansion on either side.

ex <- expand_scale(mult = c(0, 0.05), add = c(0, 0))
ggplot(transport, aes(x = gender, y = height, fill = gender)) +
  geom_boxplot() +
  theme_bw() +
  scale_y_continuous(expand = ex) 
  • The input to expand_scale() adjusts bottom/top or left/right depending on your axis.

Other plot attributes

Fill and Outline Colours

ggplot(transport, aes(x = gender, y = height, fill = gender)) +
  geom_boxplot() +
  theme_bw() +
  scale_fill_manual(values = c("green", "blue"))

Colours can also be specified using hexadecimal codes

rgb(1, 0, 0)

[1] "#FF0000"

Saving Images

Exporting Figures

Using ggsave()

The main image formats are jpeg, png and tiff

  • R can also export svg and pdf
  • ggplot2 has the function ggsave()
?ggsave

The Plots Tab is the default graphics device

Exporting Figures

Using ggsave()

ggplot(transport, aes(x = gender, y = height, fill = gender)) +
  geom_boxplot() +
  theme_bw() +
  scale_fill_manual(values = c("green", "blue"))
ggsave("HeightByGender.png")
  • ggsave defaults to 300dpi
  • Defaults to the size of the Plots Tab

Exporting Figures

Using ggsave()

  • Change the size manually by setting the width and height attributes
ggsave("HeightByGender.png", width = 18, height = 18,
       units = "cm")
  • Getting the font size right can take ages
  • Need to set correctly when we make the plot

Exporting Figures

Writing Directly

  • We initiate another graphics device using png(), jpeg(), pdf() etc.
  • Turn the device off after creating the image using dev.off()
  • Nothing will appear in the Plots tab

Exporting Figures

Writing Directly

png("HeightByGender.png", width = 18, height = 18, 
    units = "cm", res = 300)
ggplot(transport, aes(x = gender, y = height, fill = gender)) +
  geom_boxplot() +
  theme_bw() +
  theme(text = element_text(size = 16)) +
  scale_fill_manual(values = c("green", "blue"))
dev.off()

Exporting Figures

Writing Directly

Try to decide how big the plot will be in your final document

  • Export using that size
  • pdf and svg output are vector-based not pixel-based
  • Scale very well with size

Exercises

Exercises

  • Try loading and exploring the additional files:
  • toothData, PCR and RealTimeData