0

我创建了自己的主题,以便我和我的同事节省大量时间:)。我正在寻找一种方法来固定从 x 轴边缘到主题函数中的条的距离。当我运行下面的脚本时,y 轴和第一个条之间的距离比我想要的要大。我的目标是创建一个看起来像(见图)的图形,因此与图形两侧的距离非常小。

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

图形左右边缘到条形的距离小

4

0 回答 0