88# ' @param grid,axis,ticks 'x', 'y', 'xy' or 'none' to determine for which axes
99# ' the attribute should be drawn. Grid defaults to 'y', axis to 'x', and ticks
1010# ' to 'xy'.
11- # ' @param legend 'right', 'left', 'top', 'bottom', or 'none' to determine the
12- # ' position of the legend. Defaults to 'right'.
11+ # ' @param legend 'right', 'left', 'top', 'bottom' or 'none' to determine the
12+ # ' position of the legend. This can also be followed by a justification along
13+ # ' that side (top, bottom, left, right or centre) e.g. 'top-left',
14+ # ' 'left-bottom', 'right-centre'. Defaults to 'right'.
1315# '
1416# ' @param axis_text,axis_title 'x', 'y', 'xy' or 'none' to determine whether axis text and/or axis titles should be displayed.
1517# ' Text defaults to 'xy', as does title. Note that axis text refers to the 'labels' under the tick marks.
2931# '
3032# ' @export
3133
32-
3334theme_af <- function (
3435 base_size = getOption(" afcharts.base_size" , 14 ),
3536 base_line_size = getOption(" afcharts.base_line_size" , base_size / 24 ),
@@ -46,11 +47,29 @@ theme_af <- function(
4647 grid <- match.arg(grid , c(" y" , " x" , " xy" , " none" ))
4748 axis <- match.arg(axis , c(" x" , " y" , " xy" , " none" ))
4849 ticks <- match.arg(ticks , c(" xy" , " x" , " y" , " none" ))
49- legend <- match.arg(legend , c(" right" , " left" , " top" , " bottom" , " none" ))
50+ legend <- match.arg(
51+ legend ,
52+ c(" right" , " left" , " top" , " bottom" , " none" ,
53+ " right-top" , " right-centre" , " right-bottom" ,
54+ " left-top" , " left-centre" , " left-bottom" ,
55+ " top-left" , " top-centre" , " top-right" ,
56+ " bottom-left" , " bottom-centre" , " bottom-right" )
57+ )
58+
5059 axis_text <- match.arg(axis_text , c(" xy" , " x" , " y" , " none" ))
5160 axis_title <- match.arg(axis_title , c(" xy" , " x" , " y" , " none" ))
5261 legend_title <- match.arg(legend_title , c(" show" , " none" ))
5362
63+
64+ # Set legend position and justification based on legend arg
65+ legend_position <- stringr :: str_extract(legend , " ^[^-]*" )
66+ legend_justification <- stringr :: str_extract(legend , " (?<=-).*$" )
67+
68+ if (is.na(legend_justification )) {
69+ legend_justification <- " centre"
70+ }
71+
72+
5473 # Set colours
5574 light_grey <- " #d9d9d9"
5675
@@ -220,9 +239,9 @@ theme_af <- function(
220239 legend.text.align = NULL ,
221240 legend.title = title_for_legend ,
222241 legend.title.align = NULL ,
223- legend.position = legend ,
242+ legend.position = legend_position ,
224243 legend.direction = NULL ,
225- legend.justification = " centre " ,
244+ legend.justification = legend_justification ,
226245 legend.box = NULL ,
227246 legend.box.margin = ggplot2 :: margin(0 , 0 , 0 , 0 , " cm" ),
228247 legend.box.background = ggplot2 :: element_blank(),
0 commit comments