# generate artificial data
set.seed(4321)
x <- (1:100) / 10
y <- x + rnorm(length(x))
my.data <- data.frame(x = x,
y = y,
y.desc = - y,
group = c("A", "B"))
# by default only R is displayed
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_correlation()
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_correlation(small.r = TRUE)
ggplot(my.data, aes(x, y.desc)) +
geom_point() +
stat_correlation(label.x = "right")
# non-default methods
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_correlation(method = "kendall")
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_correlation(method = "spearman")
# use_label() can map a user selected label
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_correlation(use_label("R2"))
# use_label() can assemble and map a combined label
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_correlation(use_label("R", "P", "n", "method"))
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_correlation(use_label("R", "R.CI"))
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_correlation(use_label("R", "R.CI"),
r.conf.level = 0.95)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_correlation(use_label("R", "R.CI"),
method = "kendall",
r.conf.level = 0.95)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_correlation(use_label("R", "R.CI"),
method = "spearman",
r.conf.level = 0.95)
# f_use_label() provides additional flexibility
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_correlation(
f_use_label("R", "R.CI",
format = "\"Estimate: \"*%s*\" with confidence \"*%s"),
method = "spearman", r.conf.level = 0.95)
# manually assemble and map a specific label using paste() and aes()
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_correlation(aes(label =
after_stat(
paste(r.label, p.value.label, n.label,
sep = "*\", \"*"))))
# manually format and map a specific label using sprintf() and aes()
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_correlation(aes(label =
after_stat(
sprintf("%s*\" with \"*%s*\" for \"*%s",
r.label, p.value.label, t.value.label))))
# Inspecting the returned data using geom_debug_group()
# This provides a quick way of printing the returned data frame.
gginnards.installed <- requireNamespace("gginnards", quietly = TRUE)
if (gginnards.installed)
library(gginnards)
# the whole of computed data
if (gginnards.installed)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_correlation(geom = "debug_group")
if (gginnards.installed)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_correlation(geom = "debug_group", method = "pearson")
if (gginnards.installed)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_correlation(geom = "debug_group", output.type = "numeric")
if (gginnards.installed)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_correlation(geom = "debug_group", output.type = "markdown")
if (gginnards.installed)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_correlation(geom = "debug_group", output.type = "LaTeX")
Run the code above in your browser using DataLab