1111# ' @param legend 'right', 'left', 'top', 'bottom', or 'none' to determine the
1212# ' position of the legend. Defaults to 'right'.
1313# '
14+ # ' @param axis_text,axis_title 'x', 'y', 'xy' or 'none' to determine whether axis text and/or axis titles should be displayed.
15+ # ' Text defaults to 'xy', as does title. Note that axis text refers to the 'labels' under the tick marks.
16+ # '
17+ # ' @param legend_title Set to 'none' to suppress legend titles.
18+ # '
1419# ' @returns ggplot2 plot theme
1520# '
1621# ' @examples
@@ -30,12 +35,18 @@ theme_af <- function(base_size = 14,
3035 grid = c(" y" , " x" , " xy" , " none" ),
3136 axis = c(" x" , " y" , " xy" , " none" ),
3237 ticks = c(" xy" , " x" , " y" , " none" ),
33- legend = c(" right" , " left" , " top" , " bottom" , " none" )) {
38+ legend = c(" right" , " left" , " top" , " bottom" , " none" ),
39+ axis_text = c(" xy" , " x" , " y" , " none" ),
40+ axis_title = c(" xy" , " x" , " y" , " none" ),
41+ legend_title = c(" show" , " none" )) {
3442
3543 grid <- match.arg(grid )
3644 axis <- match.arg(axis )
3745 ticks <- match.arg(ticks )
3846 legend <- match.arg(legend )
47+ axis_text <- match.arg(axis_text )
48+ axis_title <- match.arg(axis_title )
49+ legend_title <- match.arg(legend_title )
3950
4051 # Set colours
4152 light_grey <- " #d9d9d9"
@@ -70,6 +81,81 @@ theme_af <- function(base_size = 14,
7081 ticks_x <- if (ticks %in% c(" x" , " xy" )) axis_ticks else no_ticks
7182 ticks_y <- if (ticks %in% c(" y" , " xy" )) axis_ticks else no_ticks
7283
84+
85+ # Set axis x text dependent on text arg, and define placement
86+ axis_text_x <- ggplot2 :: element_text(
87+ margin = ggplot2 :: margin(t = 0.8 * half_line / 2 ),
88+ vjust = 1
89+ )
90+ axis_text_x_top <- ggplot2 :: element_text(
91+ margin = ggplot2 :: margin(b = 0.8 * half_line / 2 ),
92+ vjust = 0
93+ )
94+ no_axis_text_x <- ggplot2 :: element_blank()
95+ axis_text_for_x <-
96+ if (axis_text %in% c(" x" , " xy" )) axis_text_x else no_axis_text_x
97+ axis_text_for_x_top <-
98+ if (axis_text %in% c(" x" , " xy" )) axis_text_x_top else no_axis_text_x
99+
100+
101+ # Set axis y text dependent on text arg, and define placement
102+ axis_text_y <- ggplot2 :: element_text(
103+ margin = ggplot2 :: margin(r = 0.8 * half_line / 2 ),
104+ hjust = 1
105+ )
106+ axis_text_y_right <- ggplot2 :: element_text(
107+ margin = ggplot2 :: margin(l = 0.8 * half_line / 2 ),
108+ hjust = 0
109+ )
110+ no_axis_text_y <- ggplot2 :: element_blank()
111+ axis_text_for_y <-
112+ if (axis_text %in% c(" y" , " xy" )) axis_text_y else no_axis_text_y
113+ axis_text_for_y_right <-
114+ if (axis_text %in% c(" y" , " xy" )) axis_text_y_right else no_axis_text_y
115+
116+
117+ # Set axis x title dependent on title arg, and define placement
118+ axis_title_x <- ggplot2 :: element_text(
119+ margin = ggplot2 :: margin(t = half_line / 2 ),
120+ vjust = 1
121+ )
122+ axis_title_x_top <- ggplot2 :: element_text(
123+ margin = ggplot2 :: margin(b = half_line / 2 ),
124+ vjust = 0
125+ )
126+ no_axis_title_x <- ggplot2 :: element_blank()
127+ axis_title_for_x <-
128+ if (axis_title %in% c(" x" , " xy" )) axis_title_x else no_axis_title_x
129+ axis_title_for_x_top <-
130+ if (axis_title %in% c(" x" , " xy" )) axis_title_x_top else no_axis_title_x
131+
132+
133+ # Set axis y title dependent on title arg, and define placement
134+ axis_title_y <- ggplot2 :: element_text(
135+ angle = 0 ,
136+ margin = ggplot2 :: margin(r = half_line / 2 ),
137+ vjust = 1 ,
138+ hjust = 0.5
139+ )
140+ axis_title_y_right <- ggplot2 :: element_text(
141+ angle = 0 ,
142+ margin = ggplot2 :: margin(l = half_line / 2 ),
143+ vjust = 1 ,
144+ hjust = 0.5
145+ )
146+ no_axis_title_y <- ggplot2 :: element_blank()
147+ axis_title_for_y <-
148+ if (axis_title %in% c(" y" , " xy" )) axis_title_y else no_axis_title_y
149+ axis_title_for_y_right <-
150+ if (axis_title %in% c(" y" , " xy" )) axis_title_y_right else no_axis_title_y
151+
152+
153+ # Set whether legend title displays or not, dependent on legend_title arg
154+ leg_title <- ggplot2 :: element_text(hjust = 0 )
155+ no_leg_title <- ggplot2 :: element_blank()
156+ title_for_legend <- if (legend_title == " show" ) leg_title else no_leg_title
157+
158+
73159 ggplot2 :: theme(
74160
75161 # Set parent characteristics
@@ -107,46 +193,19 @@ theme_af <- function(base_size = 14,
107193 axis.line.x = axis_x ,
108194 axis.line.y = axis_y ,
109195 axis.text = NULL ,
110- axis.text.x = ggplot2 :: element_text(
111- margin = ggplot2 :: margin(t = 0.8 * half_line / 2 ),
112- vjust = 1
113- ),
114- axis.text.x.top = ggplot2 :: element_text(
115- margin = ggplot2 :: margin(b = 0.8 * half_line / 2 ),
116- vjust = 0
117- ),
118- axis.text.y = ggplot2 :: element_text(
119- margin = ggplot2 :: margin(r = 0.8 * half_line / 2 ),
120- hjust = 1
121- ),
122- axis.text.y.right = ggplot2 :: element_text(
123- margin = ggplot2 :: margin(l = 0.8 * half_line / 2 ),
124- hjust = 0
125- ),
196+ axis.text.x = axis_text_for_x ,
197+ axis.text.x.top = axis_text_for_x_top ,
198+ axis.text.y = axis_text_for_y ,
199+ axis.text.y.right = axis_text_for_y_right ,
126200 axis.ticks = NULL ,
127201 axis.ticks.x = ticks_x ,
128202 axis.ticks.y = ticks_y ,
129203 axis.ticks.length = ggplot2 :: unit(half_line / 2 , " pt" ),
130- axis.title.x = ggplot2 :: element_text(
131- margin = ggplot2 :: margin(t = half_line / 2 ),
132- vjust = 1
133- ),
134- axis.title.x.top = ggplot2 :: element_text(
135- margin = ggplot2 :: margin(b = half_line / 2 ),
136- vjust = 0
137- ),
138- axis.title.y = ggplot2 :: element_text(
139- angle = 0 ,
140- margin = ggplot2 :: margin(r = half_line / 2 ),
141- vjust = 1 ,
142- hjust = 0.5
143- ),
144- axis.title.y.right = ggplot2 :: element_text(
145- angle = 0 ,
146- margin = ggplot2 :: margin(l = half_line / 2 ),
147- vjust = 1 ,
148- hjust = 0.5
149- ),
204+ axis.title.x = axis_title_for_x ,
205+ axis.title.x.top = axis_title_for_x_top ,
206+ axis.title.y = axis_title_for_y ,
207+ axis.title.y.right = axis_title_for_y_right ,
208+
150209
151210 # Legend
152211 legend.background = ggplot2 :: element_rect(colour = NA ),
@@ -156,7 +215,7 @@ theme_af <- function(base_size = 14,
156215 legend.key.size = ggplot2 :: unit(1.2 , " lines" ), # CHECK
157216 legend.text = ggplot2 :: element_text(size = ggplot2 :: rel(1 )),
158217 legend.text.align = NULL ,
159- legend.title = ggplot2 :: element_text( hjust = 0 ) ,
218+ legend.title = title_for_legend ,
160219 legend.title.align = NULL ,
161220 legend.position = legend ,
162221 legend.direction = NULL ,
0 commit comments