Methods ggplot2
Guide
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:
- All theme elements are black and white
- No lines around plot or grid lines, only black axes lines
- Same font as the paper the plot will be included in, so font is an argument of the function
- Axis labels must be very large, so size of them can be manually altered in the function
The function takes the following arguments:
font = "nameOfFont"default = NA, see next section for font names you can use.base_size = ndefault = 12, this will be the size of the produced text.
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:
- Bars in bar graphs must touch the axis: add
scale_y_continuous(expand = c(0, 0))to your ggplot for vertical bar plot, andscale_x_continuous(expand = c(0, 0))to your ggplot for a horizontal bar plot. - The color of points, bars, etc. must be black:
specify in
geom_bar()withfill = "black"orgeom_pointwithcolor = "black". - The p-value and test statistics must be displayed:
create objects for your
p_valueandtest_statisticand useannotate()to do add them to your plot. - All elements of the plot must be the same font:
when you add text with
annotate()orstat_compare_means(), you have to specifyfamily = "nameOfFont"andsize = ninside each element you add.
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 themeHow 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)