Methods ggplot2 Guide

Alexis Provencal

Alexis Provencal

Introduction

This is a guide to making plots using ggplot2 and ggpubr in Methods! You may be familiar with theme_minimal() or theme_classic() for ggplot2, this function works in the same way. Just copy and paste the function chunk to your R Markdown and use when needed.

There are many aesthetic requirements for data visualizations in Methods. This theme only meets some of them because in ggplot2, the theme() function is only used to customize the non-data elements of a plot.

Note: this theme only works with ggplot2 and ggpubr plots and the function needs to be in your markdown to use it.

Aesthetic requirements the function meets:

The function takes the following arguments:

Fonts

There are font options that are available in base R. The most relevant are "Times" and "sans", which are Times New Roman and Arial respectively. There are many other font options, but libraries must be installed.

Aesthetic requirements the function does not meet:

The function:

library(ggplot2)

methodsTheme = 
  function(font = NA, base_size = 12){
  theme(
    #Text font
    text = element_text(family = font, size = base_size),
    
    #Color
    line = element_line(color = "black"),
    panel.background = element_rect(fill = "white"), #fill plot background
    legend.key = element_rect(fill = "white"), #fill background of legend key
    axis.line = element_line(color = "black"), #color axes lines
    
    #Positioning
    legend.position = "right", #change legend position
      #options: none, left, right, bottom, top
    
    #Text sizes and options
    plot.title = element_text(
      size = base_size*1.3, #title size
      face = "plain"), #options: plain, italic, bold, bold.italic
    axis.title = element_text(size = base_size), #axis titles text size
    axis.text = element_text(size = base_size, color = "black"), #axis labels/tick numbers text size
    legend.text = element_text(size = base_size), #legend text size
    legend.title = element_text(size = base_size) #legend title text size
  )
}

Note: the name of the function can be changed from methodsTheme to anything you please! And, any other aspect of the function can be changed. Use ChatGPT or look at the documentation for assistance in changing the function preform how you would like.

How to use the function?

The function takes in two arguments font and base_size. If base_size is not specified, it will default to 12 pt. To use the function:

ggplot(data, aes(x = Species, y = meanSepalLength)) +
  geom_bar(stat = "identity", position = "dodge", fill = "black", #fill black
           width = 0.7) + #changes width of bars
  labs(x = "Species",
       y = "Mean Sepal Length") +
  scale_y_continuous(expand = c(0, 0)) + #makes bars touch axis
  methodsTheme(font = "Times", base_size = 24) #using custom theme

How do functions work?

Functions take in arguments and return something. In this case, the function takes in the arguments font and base_size and returns a theme that you can add to your ggplot. You can see the arguments of a function and their default values here: function(font = NA, base_size = 12){}. Arguments have default values so that they run without the user needing to specify them. But, beware that you will need to specify arguments to fully take advantage of the capabilities of this function.

ggpubr

library(ggplot2)
library(ggpubr)
library(ggsignif)

myfont = "Times"

# Create a ggviolin plot with ggpubr
ggviolin(iris, x = "Species", y = "Sepal.Length", fill = "gray") +
  labs(x = "Species",
       y = "Sepal Length") +
  geom_boxplot(width = 0.2, fill = "white", color = "black") +
  stat_compare_means(method = "anova", family = myfont, size = 8) +
  methodsTheme(font = myfont, base_size = 24)