Creating publication-worthy figures in R

A popular question I am asked is: “How can I use R to create figures for my thesis and/ or publications?” I used R to create figures for my (now submitted!) PhD thesis and a recent publication in Journal of Sports Sciences. The code below will give you high-resolution figures that you can also use in your own scientific work!

First, create a dummy dataset so we can get plotting:

# Create a list of netball athlete positions
PlayingPosition = c("C", "WA", "WD", "GA", "GD", "GS", "GK")
# Declare two playing standards, elite and junior-elite
PlayingStandard = c("Elite", "Junior Elite")
# Set the seed, to reuse the same set of random variables
set.seed(14)
# Create a summary data.frame containing dummy total distance data, across both playing standards
SummaryData = data.frame(PlayingPosition = rep((PlayingPosition), each = 2),
PlayingStandard = rep((PlayingStandard), each = 1),
TotalDistance = runif(14, 450, 950))
# Round the total distance column to 0 decimal places
SummaryData\$TotalDistance <- round(SummaryData\$TotalDistance, digits = 0)

Next, load the required package and run the code to create the figure:

# Load required package
require(ggplot2)
# Creating a publication worthy figure
ggplot(data = SummaryData, aes(x = PlayingPosition, y = TotalDistance, fill = PlayingStandard)) +
geom_bar(width=.7, stat="identity", position="dodge", colour="Black") +
scale_fill_grey() +
theme_bw() +
ylab("Total Distance (m) \n") +
scale_y_continuous(limits = c(0, 1000), expand = c(0, 0), breaks = seq(0, 1000, by = 250)) +
theme(legend.position = "bottom",
legend.title = element_blank(),
axis.text.x = element_text(size = 12, colour = "black"),
axis.text.y = element_text(size = 12, colour = "black"),
axis.ticks.x = element_blank(),
plot.background = element_blank(),
plot.title = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_text(face = "bold", size = 14),
axis.line.x = element_line(color = "black", size = .5),
axis.line.y = element_line(color = "black", size = .5),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank()) Although this figure looks OK, the figure legend is plotted. This is important, so the colours associated with elite and junior-elite levels are known however, legends are typically not included in scientific work. Rather, the colours and levels are included in the accompanying figure caption. Run the code below to remove this figure legend completely. Alternatively, if you wish to retain the figure legend, you can alter the position by replacing legend.position = "bottom" with legend.position = "right"

# Remove the legend
ggplot(data = SummaryData, aes(x = PlayingPosition, y = TotalDistance, fill = PlayingStandard)) +
geom_bar(width=.7, stat="identity", position="dodge", colour="Black") +
scale_fill_grey() +
theme_bw() +
ylab("Total Distance (m) \n") +
scale_y_continuous(limits = c(0, 1000), expand = c(0, 0), breaks = seq(0, 1000, by = 250)) +
theme(legend.position = "none",
legend.title = element_blank(),
axis.text.x = element_text(size = 12, colour = "black"),
axis.text.y = element_text(size = 12, colour = "black"),
axis.ticks.x = element_blank(),
plot.background = element_blank(),
plot.title = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_text(face = "bold", size = 14),
axis.line.x = element_line(color = "black", size = .5),
axis.line.y = element_line(color = "black", size = .5),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank()) Although this figure will look great in the “Plots” section of RStudio, it does not export as a high-resolution figure. Most journals will require figures in either .png or .TIFF format and thankfully, we can do this in R!

To export a high-resolution figure to your Desktop, we must first assign the plot then use another package called gridExtra. You may need to first install this package before running the code below. If you work on a Windows machine, you will also need to change the file location. Mac users – the code below should be applicable!

# To export a high-resolution figure
Figure1 <- ggplot(data = SummaryData, aes(x = PlayingPosition, y = TotalDistance, fill = PlayingStandard)) +
geom_bar(width=.7, stat="identity", position="dodge", colour="Black") +
scale_fill_grey() +
theme_bw() +
ylab("Total Distance (m) \n") +
scale_y_continuous(limits = c(0, 1000), expand = c(0, 0), breaks = seq(0, 1000, by = 250)) +
theme(legend.position = "none",
legend.title = element_blank(),
axis.text.x = element_text(size = 12, colour = "black"),
axis.text.y = element_text(size = 12, colour = "black"),
axis.ticks.x = element_blank(),
plot.background = element_blank(),
plot.title = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_text(face = "bold", size = 14),
axis.line.x = element_line(color = "black", size = .5),
axis.line.y = element_line(color = "black", size = .5),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank())
Figure1 # Load required package
require(gridExtra)