#' @title Plot the confidence interval for correlation
#'
#' @description
#' `r badge('stable')`
#'
#'This function plots the 95% confidence interval for Pearson's correlation
#'coefficient generated by the function `corr_ci`.
#'
#' @param object An object generate by the function `corr_ci()`
#' @param fill If `corr_ci()` is computed with the argument `by` use
#' `fill` to fill the shape by each level of the grouping variable
#' `by`.
#' @param position.fill The position of shapes and errorbar when `fill` is used.
#' Defaults to `0.3`.
#' @param x.lab The label of x-axis, set to 'Pairwise combinations'. New
#' arguments can be inserted as `x.lab = 'my label'`.
#' @param y.lab The label of y-axis, set to 'Pearson's correlation coefficient'
#' New arguments can be inserted as `y.lab = 'my label'`.
#' @param y.lim The range of x-axis. Default is `NULL`. The same arguments
#' than `x.lim` can be used.
#' @param y.breaks The breaks to be plotted in the x-axis. Default is
#' `authomatic breaks`. The same arguments than `x.breaks` can be
#' used.
#' @param shape The shape point to represent the correlation coefficient.
#' Default is `21` (circle). Values must be between `21-25`:
#' `21` (circle), `22` (square), `23` (diamond), `24` (up
#' triangle), and `25` (low triangle).
#' @param col.shape The color for the shape edge. Set to `black`.
#' @param fill.shape The color to fill the shape. Set to `orange`.
#' @param size.shape The size for the shape point. Set to `2.5`.
#' @param width.errbar The width for the errorbar showing the CI.
#' @param main The title of the plot. Set to `main = FALSE` to ommite the
#' plot title.
#' @param invert.axis Should the names of the pairwise correlation appear in the
#' y-axis?
#' @param reorder Logical argument. If `TRUE` (default) the pairwise
#' combinations are reordered according to the correlation coefficient.
#' @param legend.position The position of the legend when using `fill` argument.
#' Defaults to `"bottom"`.
#' @param plot_theme The graphical theme of the plot. Default is
#' `plot_theme = theme_metan()`. For more details, see
#' [ggplot2::theme()].
#' @return An object of class `gg, ggplot`.
#' @md
#' @export
#' @examples
#' \donttest{
#' library(metan)
#' library(dplyr)
#' # Traits that contains "E"
#' data_ge2 %>%
#' select(contains('E')) %>%
#' corr_ci() %>%
#' plot_ci()
#'
#' # Group by environment
#' # Traits PH, EH, EP, EL, and ED
#' # Select only correlations with PH
#'
#'data_ge2 %>%
#' corr_ci(PH, EP, EL, ED, CW,
#' sel.var = "PH",
#' by = ENV) %>%
#' plot_ci(fill = ENV)
#'}
plot_ci <- function(object,
fill = NULL,
position.fill = 0.3,
x.lab = NULL,
y.lab = NULL,
y.lim = NULL,
y.breaks = waiver(),
shape = 21,
col.shape = "black",
fill.shape = "orange",
size.shape = 2.5,
width.errbar = 0.2,
main = TRUE,
invert.axis = TRUE,
reorder = TRUE,
legend.position = "bottom",
plot_theme = theme_metan()) {
if (!has_class(object, "tbl_df")) {
stop("The object must be a 'data.frame' or a 'tbl_df'.")
}
if (!any(colnames(object) %in% c("Pair", "Corr"))) {
stop("It appers that the object was not generate by the function 'coor_ci()'.")
}
n <- object[1, which(colnames(object) == "n")]
x.lab <- ifelse(is.null(x.lab) == F, x.lab, paste0("Pairwise combinations"))
y.lab <- ifelse(is.null(y.lab) == F, y.lab, paste0("Pearson's correlation coefficient"))
object %<>% mutate(Pair = paste(V1, V2, sep = " x "))
if(reorder == TRUE){
if(!missing(fill)){
p <-
ggplot(object, aes(x = reorder(Pair, Corr), y = Corr, fill = {{fill}})) +
geom_errorbar(aes(ymax = UL,
ymin = LL),
width = width.errbar,
position = position_dodge(width = position.fill)) +
geom_point(col = col.shape,
position = position_dodge(width = position.fill),
size = size.shape,
shape = shape)
} else{
p <-
ggplot(object, aes(x = reorder(Pair, Corr), y = Corr)) +
geom_errorbar(aes(ymax = UL,
ymin = LL),
width = width.errbar) +
geom_point(col = col.shape,
fill = fill.shape,
size = size.shape,
shape = shape)
}
} else{
if(!missing(fill)){
p <-
ggplot(object, aes(x = Pair, y = Corr, fill = {{fill}})) +
geom_errorbar(aes(ymax = UL,
ymin = LL),
width = width.errbar,
position = position_dodge(width = position.fill)) +
geom_point(col = col.shape,
position = position_dodge(width = position.fill),
size = size.shape,
shape = shape)
} else{
p <-
ggplot(object, aes(x = Pair, y = Corr)) +
geom_errorbar(aes(ymax = UL,
ymin = LL),
width = width.errbar) +
geom_point(col = col.shape,
fill = fill.shape,
size = size.shape,
shape = shape)
}
}
p <- p + geom_hline(yintercept = 0, linetype = "dashed")
if (invert.axis == TRUE) {
p <- p + coord_flip()
} else {
p <- p
}
p <- p + plot_theme %+replace%
theme(axis.text = element_text(colour = "black"),
legend.position = legend.position) +
labs(x = x.lab, y = y.lab)
if (main == TRUE) {
p <- p + ggtitle("95% CI for Pearson's correlation coefficient",
subtitle = paste("n = ", n))
}
return(p)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.