If you are doing any work related to statistics, you will eventually need to have a plot drawn. One of the important things to consider when plotting a plot is how the plot looks. The design of the plot is at least as important as the information it provides, because it may not be possible to read information from a poorly designed plot. Designing a plot can be long and challenging. Fortunately, some plot themes are available both in ggplot2 package and in additional packages like ggthemes. However, let’s say you want to make your own theme; is this possible? Of course it is! One of the functions in ggplot2 package, you can make your own theme and use it in every project. In this post, I will explain how you can create your own theme in R.


theme() function in ggplot2 package is used to customize the appearance of the drawn plots. It is a comprehensive function with dozens of arguments, although I don’t know exactly how many. Below you can find some widely used arguments and what they do. title: it is used to set the appearance of the chart title. For example, title = element_text(size = 14, face = “bold”) sets the font size of the title to 14 and the boldness to bold. axis.title: it is used to set the appearance of axis titles. For example, axis.title.x = element_text(size = 12) sets the font size of the x axis title to 12. axis.text: it is used to set the appearance of axis texts. For example, axis.text.y = element_text(angle = 90, hjust = 0.5) rotates the y axis texts 90 degrees and centers the horizontal alignment. legend.title: it is used to set the appearance of the legend title legend.text: it is used to set the appearance of the legend texts. panel.background: it is used to set the background of the drawing panel. panel.grid: it is used to set the appearance of the grids in the drawing panel.

Following code chunk shows how default line plot looks like:

library(ggplot2) # loading library

creating a data frame

xValue <- 1:10 yValue <- cumsum(rnorm(10)) data <- data.frame(xValue,yValue)

plot

ggplot(data, aes(x=xValue, y=yValue)) + geom_line()