Customising your graph

So far, we have explore some basic plots and functions of ggplot. But, we have not explored how truly powerful and customisable ggplot is. This section is designed to show you a wealth of customisable options for the general appearance of your graph. Alot of this section is code-heavy and can get quite overwhelming but thankfully, alot of the code is repetitive. So remember, take a break, grab a coffee and proceed through the customisation section at your own pace. Alot of this isn’t critical to producing a simple graph and you can move on to the next section of plotting at any time.


  • Themes
  • The easiest way to quickly modify your graph is to add one of the preset theme() commands. I will add each of them to the graph which will replace the previous theme. We can simply add items to our current graph object by adding the + sign. Keep in mind that if you dont “resave” it to the object, it wont stick around. If you want to keep a theme, either add it into the original ggplot command, or save it to the same or a new object.

  • Axis lines
  • To change the axis lines and ticks (lines above each number on an axis) use the following. Theme argument Description axis.line = element_line(insert changes here) This will change both axes lines. axis.line.x = element_line(insert changes here) This will change just the x axis. axis.line.y = element_line(insert changes here) This will change just the y axis. axis.ticks = element_line(insert changes here) Change both axes ticks.

  • Background
  • The plot and legend background colours can be changed using the following: Theme argument Description panel.background = element_rect(insert changes here) This changes the background of the main plot itself. We need element_rect() as it is a rectangle geometric object. legend.background = element_rect(insert changes here) This will change the main area of the legend. legend.key = element_rect(insert changes here) This will change the small boxes that each of the factors levels are identified with.

  • Grids
  • So far, our graph does not have the original ggplot grid lines because we removed them in our original graph. Before we start changing these, let’s save our beautiful masterpiece to an object/variable to simplify the theme() changing. iris.scatter <- iris.scatter + theme(panel.background = element_rect(fill="lavender", colour="red"), legend.background = element_rect(fill="lavender", colour="yellow", size=1), legend.key = element_rect(fill = "gray50", colour = "green", size = 0.5)) + theme(axis.line.x = element_line(colour = "skyblue", size=2), axis.line.y = element_line(colour="deeppink", size = 2), axis.

  • Axis labels
  • If we want to change the axis labels themselves, this is done using the labs() command. iris.scatter <- iris.scatter + labs(x = "Sepal Length (cm)", y = "Petal Length (cm)") iris.scatter If we wish to add a title to our plot (not overly common in publications) we can use the following. iris.scatter <- iris.scatter + labs(title= "Relationship between petal and sepal length") iris.scatter After trying to use these labs() commands you will start to realise it hates anything slightly symbolic (subscript, superscript, degrees etc.

  • Proper examples
  • ## Setting up the graph environment ## iris.scatter.proper <- ggplot(iris, aes(x=Sepal.Length, y=Petal.Length, colour=Species, shape=Species)) + geom_point() ## Making our theme ## plottheme <- theme(panel.background = element_rect(fill="ghostwhite"), legend.background = element_blank(), legend.key = element_rect(fill="ghostwhite"), axis.line = element_line(colour="black", size=1), axis.ticks = element_blank(), axis.title = element_text(colour="royalblue3", size=14), plot.title = element_text(face="bold", colour="steelblue4", size=16), legend.title = element_text(colour="royalblue3", size=14), legend.text = element_text(face="italic", colour="steelblue4", size=10), axis.text = element_text(colour="steelblue4", size=12), panel.grid.major = element_line(colour="gray80"), panel.grid.minor = element_blank()) ## Applying the theme, adding some labels and changing some colours ## iris.