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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/geom_bracket.R
\name{stat_bracket}
\alias{stat_bracket}
\alias{geom_bracket}
\title{Add Brackets with Labels to a GGPlot}
\usage{
stat_bracket(
mapping = NULL,
data = NULL,
position = "identity",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
label = NULL,
type = c("text", "expression"),
y.position = NULL,
xmin = NULL,
xmax = NULL,
step.increase = 0,
step.group.by = NULL,
tip.length = 0.03,
bracket.nudge.y = 0,
bracket.shorten = 0,
size = 0.3,
label.size = 3.88,
family = "",
vjust = 0,
...
)
geom_bracket(
mapping = NULL,
data = NULL,
stat = "bracket",
position = "identity",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
label = NULL,
type = c("text", "expression"),
y.position = NULL,
xmin = NULL,
xmax = NULL,
step.increase = 0,
step.group.by = NULL,
tip.length = 0.03,
bracket.nudge.y = 0,
bracket.shorten = 0,
size = 0.3,
label.size = 3.88,
family = "",
vjust = 0,
coord.flip = FALSE,
...
)
}
\arguments{
\item{mapping}{Set of aesthetic mappings created by \code{\link[ggplot2:aes]{aes()}}. If specified and
\code{inherit.aes = TRUE} (the default), it is combined with the default mapping
at the top level of the plot. You must supply \code{mapping} if there is no plot
mapping.}
\item{data}{The data to be displayed in this layer. There are three
options:
If \code{NULL}, the default, the data is inherited from the plot
data as specified in the call to \code{\link[ggplot2:ggplot]{ggplot()}}.
A \code{data.frame}, or other object, will override the plot
data. All objects will be fortified to produce a data frame. See
\code{\link[ggplot2:fortify]{fortify()}} for which variables will be created.
A \code{function} will be called with a single argument,
the plot data. The return value must be a \code{data.frame}, and
will be used as the layer data. A \code{function} can be created
from a \code{formula} (e.g. \code{~ head(.x, 10)}).}
\item{position}{Position adjustment, either as a string naming the adjustment
(e.g. \code{"jitter"} to use \code{position_jitter}), or the result of a call to a
position adjustment function. Use the latter if you need to change the
settings of the adjustment.}
\item{na.rm}{If \code{FALSE} (the default), removes missing values with a
warning. If \code{TRUE} silently removes missing values.}
\item{show.legend}{logical. Should this layer be included in the legends?
\code{NA}, the default, includes if any aesthetics are mapped.
\code{FALSE} never includes, and \code{TRUE} always includes.
It can also be a named logical vector to finely select the aesthetics to
display.}
\item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics,
rather than combining with them. This is most useful for helper functions
that define both data and aesthetics and shouldn't inherit behaviour from
the default plot specification, e.g. \code{\link[ggplot2:borders]{borders()}}.}
\item{label}{character vector with alternative label, if not null test is
ignored}
\item{type}{the label type. Can be one of "text" and "expression" (for
parsing plotmath expression).}
\item{y.position}{numeric vector with the y positions of the brackets}
\item{xmin}{numeric vector with the positions of the left sides of the
brackets}
\item{xmax}{numeric vector with the positions of the right sides of the
brackets}
\item{step.increase}{numeric vector with the increase in fraction of total
height for every additional comparison to minimize overlap.}
\item{step.group.by}{a variable name for grouping brackets before adding
step.increase. Useful to group bracket by facet panel.}
\item{tip.length}{numeric vector with the fraction of total height that the
bar goes down to indicate the precise column}
\item{bracket.nudge.y}{Vertical adjustment to nudge brackets by. Useful to
move up or move down the bracket. If positive value, brackets will be moved
up; if negative value, brackets are moved down.}
\item{bracket.shorten}{a small numeric value in [0-1] for shortening the with
of bracket.}
\item{size}{change the width of the lines of the bracket}
\item{label.size}{change the size of the label text}
\item{family}{change the font used for the text}
\item{vjust}{move the text up or down relative to the bracket}
\item{...}{other arguments passed on to \code{\link{layer}}. These are often
aesthetics, used to set an aesthetic to a fixed value, like \code{color =
"red"} or \code{size = 3}. They may also be parameters to the paired
geom/stat.}
\item{stat}{The statistical transformation to use on the data for this
layer, either as a \code{ggproto} \code{Geom} subclass or as a string naming the
stat stripped of the \code{stat_} prefix (e.g. \code{"count"} rather than
\code{"stat_count"})}
\item{coord.flip}{logical. If \code{TRUE}, flip x and y coordinates so that
horizontal becomes vertical, and vertical, horizontal. When adding the
p-values to a horizontal ggplot (generated using
\code{\link[ggplot2]{coord_flip}()}), you need to specify the option
\code{coord.flip = TRUE}.}
}
\description{
add brackets with label annotation to a ggplot. Helpers for
adding p-value or significance levels to a plot.
}
\examples{
df <- ToothGrowth
df$dose <- factor(df$dose)
# Add bracket with labels
ggboxplot(df, x = "dose", y = "len") +
geom_bracket(
xmin = "0.5", xmax = "1", y.position = 30,
label = "t-test, p < 0.05"
)
# Customize bracket tip.length tip.length
ggboxplot(df, x = "dose", y = "len") +
geom_bracket(
xmin = "0.5", xmax = "1", y.position = 30,
label = "t-test, p < 0.05", tip.length = c(0.2, 0.02)
)
#Using plotmath expression
ggboxplot(df, x = "dose", y = "len") +
geom_bracket(
xmin = "0.5", xmax = "1", y.position = 30,
label = "list(~italic(p)<=0.001)", type = "expression",
tip.length = c(0.2, 0.02)
)
# Specify multiple brackets manually
ggboxplot(df, x = "dose", y = "len") +
geom_bracket(
xmin = c("0.5", "1"), xmax = c("1", "2"),
y.position = c(30, 35), label = c("***", "**"),
tip.length = 0.01
)
# Compute statistical tests and add p-values
stat.test <- compare_means(len ~ dose, ToothGrowth, method = "t.test")
ggboxplot(df, x = "dose", y = "len") +
geom_bracket(
aes(xmin = group1, xmax = group2, label = signif(p, 2)),
data = stat.test, y.position = 35
)
# Increase step length between brackets
ggboxplot(df, x = "dose", y = "len") +
geom_bracket(
aes(xmin = group1, xmax = group2, label = signif(p, 2)),
data = stat.test, y.position = 35, step.increase = 0.1
)
# Or specify the positions of each comparison
ggboxplot(df, x = "dose", y = "len") +
geom_bracket(
aes(xmin = group1, xmax = group2, label = signif(p, 2)),
data = stat.test, y.position = c(32, 35, 38)
)
}
|