View source: R/stat_pvalue_manual.R
stat_pvalue_manual | R Documentation |
Add manually p-values to a ggplot, such as box blots, dot plots and stripcharts. Frequently asked questions are available on Datanovia ggpubr FAQ page, for example:
How to Add P-Values onto a Grouped GGPLOT using the GGPUBR R Package
How to Create Stacked Bar Plots with Error Bars and P-values
stat_pvalue_manual( data, label = NULL, y.position = "y.position", xmin = "group1", xmax = "group2", x = NULL, size = 3.88, label.size = size, bracket.size = 0.3, bracket.nudge.y = 0, bracket.shorten = 0, color = "black", linetype = 1, tip.length = 0.03, remove.bracket = FALSE, step.increase = 0, step.group.by = NULL, hide.ns = FALSE, vjust = 0, coord.flip = FALSE, position = "identity", ... )
data |
a data frame containing statitistical test results. The expected
default format should contain the following columns: |
label |
the column containing the label (e.g.: label = "p" or label =
"p.adj"), where |
y.position |
column containing the coordinates (in data units) to be used for absolute positioning of the label. Default value is "y.position". Can be also a numeric vector. |
xmin |
column containing the position of the left sides of the brackets. Default value is "group1". |
xmax |
(optional) column containing the position of the right sides of the brackets. Default value is "group2". If NULL, the p-values are plotted as a simple text. |
x |
x position of the p-value. Should be used only when you want plot the p-value as text (without brackets). |
size, label.size |
size of label text. |
bracket.size |
Width of the lines of the bracket. |
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. |
bracket.shorten |
a small numeric value in [0-1] for shortening the with of bracket. |
color |
text and line color. Can be variable name in the data for coloring by groups. |
linetype |
linetype. Can be variable name in the data for changing linetype by groups. |
tip.length |
numeric vector with the fraction of total height that the bar goes down to indicate the precise column. Default is 0.03. |
remove.bracket |
logical, if |
step.increase |
numeric vector with the increase in fraction of total height for every additional comparison to minimize overlap. |
step.group.by |
a variable name for grouping brackets before adding step.increase. Useful to group bracket by facet panel. |
hide.ns |
can be logical value or a character vector.
|
vjust |
move the text up or down relative to the bracket. Can be also a column name available in the data. |
coord.flip |
logical. If |
position |
position adjustment, either as a string, or the result of a call to a position adjustment function. |
... |
other arguments passed to the function |
stat_compare_means
# T-test stat.test <- compare_means( len ~ dose, data = ToothGrowth, method = "t.test" ) stat.test # Create a simple box plot p <- ggboxplot(ToothGrowth, x = "dose", y = "len") p # Perform a t-test between groups stat.test <- compare_means( len ~ dose, data = ToothGrowth, method = "t.test" ) stat.test # Add manually p-values from stat.test data # First specify the y.position of each comparison stat.test <- stat.test %>% mutate(y.position = c(29, 35, 39)) p + stat_pvalue_manual(stat.test, label = "p.adj") # Customize the label with glue expression # (https://github.com/tidyverse/glue) p + stat_pvalue_manual(stat.test, label = "p = {p.adj}") # Grouped bar plots #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ToothGrowth$dose <- as.factor(ToothGrowth$dose) # Comparisons against reference stat.test <- compare_means( len ~ dose, data = ToothGrowth, group.by = "supp", method = "t.test", ref.group = "0.5" ) stat.test # Plot bp <- ggbarplot(ToothGrowth, x = "supp", y = "len", fill = "dose", palette = "jco", add = "mean_sd", add.params = list(group = "dose"), position = position_dodge(0.8)) bp + stat_pvalue_manual( stat.test, x = "supp", y.position = 33, label = "p.signif", position = position_dodge(0.8) )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.