0

I created my own theme so that me and my colleagues get saved tons of time :). I am looking for a way to fix the distance from the edges of the x-axis to the bars within the theme function. As I run the script below, the distance between the y-axis and the first bar is bigger than the distance than I would like. I aim at creating a graph that looks like (see image), so with very small distance from the sides of the graph.

library(ggplot2)
library(ggthemes)
library(plyr)

#dataset
df <- mtcars
df$cyl <- as.factor(df$cyl)
df$am <- as.factor(df$am)
df$am <- revalue(df$am, c("0"="Automatic", "1"="Manual"))

#define color set; middenblauw, donkerblauw, geel, rood, oranje, donkergroen, lichtblauw, lichtgrijs, donkergrijs
uwvPalet <- c("#0078D2","#003282","#C4D600","#BE0028","#F08C00","#687A00","#B9E2FF","#B9E2FF","#999999")

#generating new theme
theme_uwv <- 
  function(base_size = 22,                                                                 #general font size
       base_family = ""){                                                              #general font type
  theme_hc(base_size = base_size,                                                      #basic theme to start from
           base_family = base_family)   %+replace%
     theme(axis.title.x = element_blank(),                                             #no axis titles
           axis.title.y = element_blank(),
           axis.text = element_text(color = rgb(0, 0, 0,                               #axis text labels
                                    maxColorValue = 255), size = rel(0.5)),
           axis.ticks = element_blank(),                                               #no axis ticks                                      
           legend.title = element_blank(),                                             #no legend title
           legend.text = element_text(color = "black", size = 12,face = "plain"),      #legend text labels
           legend.text.align = 0.5,                                                    #legend text alignment
           legend.spacing.x = unit(0.2, 'cm'),                                         #spacing between legend categories
           panel.grid.major = element_line(rgb(105, 105, 105,maxColorValue = 255),     #only major grid lines
                                       linetype = "solid"),   
           axis.line.x = element_line(colour = 'black', size = 2),                     #thicker x-axis
           plot.title = element_text(color = rgb(0, 120, 210, maxColorValue = 255),    #titles and subtitles are added in the powerpoint presentation. However, if you do want to include them in your graph, these are the UWV standards
                                     size = 26, face = "bold", hjust = -0.05), 
           plot.subtitle = element_text(color = rgb(0, 120, 210, maxColorValue = 255),
                                        size = 18, face = "bold", hjust = -0.05),
  complete = TRUE
)
   }

#add default colour set
theme_uwv <- list(theme_uwv(), scale_fill_manual(values = uwvPalet))

# Grouped bar plot
ggplot(df, aes(fill = cyl, x = am, y = mpg)) +
  geom_col(position = position_dodge(width = 0.9)) +
  theme_uwv

Small distance from left and right edges of graph to bars

Axeman
  • 32,068
  • 8
  • 81
  • 94
SHW
  • 461
  • 7
  • 26
  • 1
    That distance is a property of the scale (parameter `expand` in `scale_x_discrete`), not the theme. – Axeman Aug 06 '19 at 16:30
  • Thank you Axeman, this was not the answer I was hoping for, but it was the answer that got me to a more elegant way of setting up my code – SHW Aug 08 '19 at 14:33

0 Answers0