-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathrotate_axis_text.R
57 lines (52 loc) · 1.42 KB
/
rotate_axis_text.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#'Rotate Axes Text
#'
#'@description Rotate the x-axis text (tick mark labels).
#'\itemize{
#'\item \code{rotate_x_text}(): Rotate x axis text.
#'\item \code{rotate_y_text}(): Rotate y axis text.
#'}
#'
#'@param angle numeric value specifying the rotation angle. Default is 90 for vertical x-axis text.
#'@param hjust horizontal justification (in [0, 1]).
#'@param vjust vertical justification (in [0, 1]).
#'@param ... other arguments to pass to the function
#' \code{\link[ggplot2:element]{element_text}()}.
#'
#'@examples
#'# Load data
#'data("ToothGrowth")
#'df <- ToothGrowth
#'
#'# Basic plot
#'p <- ggboxplot(df, x = "dose", y = "len")
#'p
#'# Vertical x axis text
#'p + rotate_x_text()
#'# Set rotation angle to 45
#'p + rotate_x_text(45)
#'p + rotate_y_text(45)
#'
#'@name rotate_axis_text
#'@rdname rotate_axis_text
#'@export
rotate_x_text <- function(angle = 90, hjust = NULL, vjust = NULL, ...){
if(missing(hjust) & angle > 5)
hjust <- 1
if(missing(vjust) & angle == 90)
vjust <- 0.5
theme(
axis.text.x = element_text(angle = angle, hjust = hjust, vjust = vjust, ...)
)
}
#'@name rotate_axis_text
#'@rdname rotate_axis_text
#'@export
rotate_y_text <- function(angle = 90, hjust = NULL, vjust = NULL, ...){
if(missing(hjust) & angle == 90)
hjust <- 0.5
else if(missing(hjust) & angle > 5)
hjust <- 1
theme(
axis.text.y = element_text(angle = angle, hjust = hjust, vjust = vjust, ...)
)
}