The default ggplot2 theme isn’t to everyone’s taste. Here’s an example of how a custom theme can be created.
First, load the ggplot2 library.
library(ggplot2)
Create/import a data set to use in the plots. In this example, I created a data frame of fictional timesheet data.
# Create a vector containing names of people
person <- c("Rob Smith", "Daisy Jones", "Steve Green", "Helen Jenkins", "Daisy Jones", "Steve Green", "Daisy Jones", "Helen Jenkins", "Rob Smith", "Helen Jenkins", "Daisy Jones", "Steve Green", "Rob Smith", "Helen Jenkins")
# Create a vector containing names of projects
project <- c("Eagle", "Oak", "Hawk", "Hawk", "Oak", "Hawk", "Hawk", "Robin", "Eagle", "Hawk", "Oak", "Hawk", "Robin", "Eagle")
# Create a vector containing numbers of hours
hours <- c(9, 8, 10, 10, 4, 5, 6, 3, 8, 9, 9, 8, 8.5, 10)
# Create a vector containing dates
submitted <- as.Date(c("2019-04-15", "2019-04-15", "2019-04-15", "2019-04-15", "2019-04-16", "2019-04-16", "2019-04-16", "2019-04-16", "2019-04-16", "2019-04-16", "2019-04-17", "2019-04-17", "2019-04-17", "2019-04-17"))
# Create a data frame using the 4 vectors where each vector is a column
timesheets <- data.frame(person, project, hours, submitted)
I then used that data to create a new data frame of total hours by date, so that it could be used to create a line plot.
# Create a data frame with aggregated hours by date
hours_by_date_data <- aggregate(timesheets$hours, by=list(timesheets$submitted),FUN = sum)
# Change the column names to "submitted" and "hours"
colnames(hours_by_date_data) <- c("submitted", "hours")
Here is a line plot using the hours by date data, using the default theme (since no theme attributes were specified):
# Create a plot showing hours by date
hours_by_date_plot <- ggplot(data=hours_by_date_data, aes(x=submitted, y=hours)) +
geom_line()+
geom_point()+
labs(x = "Date Submitted",
y = "Hours")
# Display the plot
hours_by_date_plot
Notice that this text is quite close to the plot above. Also:
I wanted to create a custom theme based on the theme_bw theme but customised to:
# create a new theme based on the theme_bw() theme
new_theme <- theme_bw()+
theme(
panel.background = element_blank(), # Remove the grey background
axis.ticks = element_blank(), # Remove the axis ticks
panel.border = element_blank(), # Remove the border
axis.line = element_line(colour = "#7f7e7e"), # Add the x and y axis lines back in
axis.title = element_text(colour = "#7f7e7e", size = 14), # Make the axis titles grey (#7f7e7e) and size 14
axis.text = element_text(colour = "#7f7e7e", size = 14), # Make the axis values text grey (#7f7e7e) and size 14
plot.margin=unit(c(1,0,1,0),"cm")) # Add white space above and below the plot
# Ask R to apply the new theme to all plots defined from this point on
theme_set(new_theme)
# Display the hours_by_date plot again and notice how the new_theme has been applied
hours_by_date_plot
Notice how there is more white space between the plot above and this text. It makes the document less “busy”, easier to read, and the plot stands out.