-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathoutput.R
More file actions
162 lines (150 loc) · 5.82 KB
/
Copy pathoutput.R
File metadata and controls
162 lines (150 loc) · 5.82 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#' Fashion a correlation data frame for printing.
#'
#' For the purpose of printing, convert a correlation data frame into a noquote
#' matrix with the correlations cleanly formatted (leading zeros removed; spaced
#' for signs) and the diagonal (or any NA) left blank.
#'
#' @param x Scalar, vector, matrix or data frame.
#' @param decimals Number of decimal places to display for numbers.
#' @param leading_zeros Should leading zeros be displayed for decimals (e.g., 0.1)? If FALSE, they will be removed.
#' @param na_print Character string indicating NA values in printed output
#' @return noquote. Also a data frame if x is a matrix or data frame.
#' @export
#' @examples
#' # Examples with correlate()
#' library(dplyr)
#' mtcars %>% correlate() %>% fashion()
#' mtcars %>% correlate() %>% fashion(decimals = 1)
#' mtcars %>% correlate() %>% fashion(leading_zeros = TRUE)
#' mtcars %>% correlate() %>% fashion(na_print = "*")
#'
#' # But doesn't have to include correlate()
#' mtcars %>% fashion(decimals = 3)
#' c(0.234, 134.23, -.23, NA) %>% fashion(na_print = "X")
fashion <- function(x, decimals = 2, leading_zeros = FALSE, na_print = "") {
UseMethod("fashion")
}
#' @export
fashion.default <- function(x, decimals = 2, leading_zeros = FALSE, na_print = "") {
# Handle numbers
if (is.numeric(x)) {
tmp <- stats::na.omit(x)
n_dig <- length(tmp)
if (n_dig) {
# Format to correct number of decimals and keep/remove any leading zeros
if (leading_zeros) {
tmp <- sprintf(paste0("%.", decimals, "f"), tmp)
} else {
tmp <- sub("^-0.", "-\\1.", sprintf(paste0("%.", decimals, "f"), tmp))
tmp <- sub("^0.", " \\1.", tmp)
}
# Pad mulitple digits to appear right justified
if (n_dig > 1) {
n_chars <- nchar(tmp)
longest <- max(n_chars)
tmp1 <- purrr::map_chr(
(longest - n_chars),
~ paste(rep(" ", .), collapse = "")
)
tmp <- paste0(tmp1, tmp)
}
# Insert back to x
x[!is.na(x)] <- tmp
}
}
x <- as.character(x)
x[is.na(x) | x == "NA"] <- na_print
noquote(x)
}
#' Plot a correlation data frame.
#'
#' Plot a correlation data frame using ggplot2.
#'
#' Each value in the correlation data frame is represented by one point/circle
#' in the output plot. The size of each point corresponds to the absolute value
#' of the correlation (via the \code{size} aesthetic). The color of each point
#' corresponds to the signed value of the correlation (via the \code{color}
#' aesthetic).
#'
#'
#' @param rdf Correlation data frame (see \code{\link{correlate}}) or object
#' that can be coerced to one (see \code{\link{as_cordf}}).
#' @param legend Boolean indicating whether a legend mapping the colors to the
#' correlations should be displayed.
#' @param shape \code{\link{geom_point}} aesthetic.
#' @param print_cor Boolean indicating whether the correlations should be
#' printed over the shapes.
#' @param colours,colors Vector of colors to use for n-color gradient.
#' @param .order Either "default", meaning x and y variables keep the same order
#' as the columns in \code{x}, or "alphabet", meaning the variables are
#' alphabetized.
#' @return Plots a correlation data frame
#' @export
#' @examples
#' x <- correlate(mtcars)
#' rplot(x)
#'
#' # Common use is following rearrange and shave
#' x <- rearrange(x, absolute = FALSE)
#' x <- shave(x)
#' rplot(x)
#' rplot(x, print_cor = TRUE)
#' rplot(x, shape = 20, colors = c("red", "green"), legend = TRUE)
rplot <- function(rdf,
legend = TRUE,
shape = 16,
colours = c("indianred2", "white", "skyblue1"),
print_cor = FALSE,
colors,
.order = c("default", "alphabet")) {
.order <- match.arg(.order)
UseMethod("rplot")
}
#' @export
rplot.default <- function(rdf, ...) {
rdf <- as_cordf(rdf)
rplot.cor_df(rdf, ...)
}
#' Network plot of a correlation data frame
#'
#' Output a network plot of a correlation data frame in which variables that are
#' more highly correlated appear closer together and are joined by stronger
#' paths. Paths are also colored by their sign (blue for positive and red for
#' negative). The proximity of the points are determined using multidimensional
#' clustering.
#'
#' @param min_cor Number from 0 to 1 indicating the minimum value of
#' correlations (in absolute terms) to plot.
#' @param legend How should the colors and legend for the correlation values be
#' displayed? The options are "full" (the default) for -1 to 1 with a legend,
#' "range" for the range of correlation values in \code{rdf} with a legend,
#' or "none" for colors between -1 to 1 with no legend displayed.
#' @param colours,colors Vector of colors to use for n-color gradient.
#' @param repel Should variable labels repel each other? If TRUE, text is added
#' via \code{\link[ggrepel]{geom_text_repel}} instead of \code{\link[ggplot2]{geom_text}}
#' @param curved Should the paths be curved? If TRUE, paths are added via
#' \code{\link[ggplot2:geom_segment]{geom_curve}}; if FALSE, via
#' \code{\link[ggplot2]{geom_segment}}
#' @inheritParams rplot
#' @export
#' @examples
#' x <- correlate(mtcars)
#' network_plot(x)
#' network_plot(x, min_cor = .1)
#' network_plot(x, min_cor = .6)
#' network_plot(x, min_cor = .2, colors = c("red", "green"), legend = "full")
#' network_plot(x, min_cor = .2, colors = c("red", "green"), legend = "range")
network_plot <- function(rdf,
min_cor = .3,
legend = c("full", "range", "none"),
colours = c("indianred2", "white", "skyblue1"),
repel = TRUE,
curved = TRUE,
colors) {
UseMethod("network_plot")
}
#' @export
network_plot.default <- function(rdf, ...) {
rdf <- as_cordf(rdf)
network_plot.cor_df(rdf, ...)
}