# 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)
# manually assemble and map a specific label using paste() and aes()
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_correlation(aes(label = paste(after_stat(r.label),
after_stat(p.value.label),
after_stat(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 = sprintf("%s*\" with \"*%s*\" for \"*%s",
after_stat(r.label),
after_stat(p.value.label),
after_stat(t.value.label))))
# Inspecting the returned data using geom_debug()
# This provides a quick way of finding out the names of the variables that
# are available for mapping to aesthetics with after_stat().
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")
if (gginnards.installed)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_correlation(geom = "debug", method = "pearson")
if (gginnards.installed)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_correlation(geom = "debug", method = "kendall")
if (gginnards.installed)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_correlation(geom = "debug", method = "spearman")
if (gginnards.installed)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_correlation(geom = "debug", output.type = "numeric")
if (gginnards.installed)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_correlation(geom = "debug", output.type = "markdown")
if (gginnards.installed)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_correlation(geom = "debug", output.type = "LaTeX")
Run the code above in your browser using DataLab