random_string <-
function(len = 6) {
paste(sample(letters, len, replace = TRUE), collapse = "")
}
# Make random data.
set.seed(1001)
d <- tibble::tibble(
x = rnorm(100),
y = rnorm(100),
group = rep(c("A", "B"), c(50, 50)),
lab = replicate(100, { random_string() })
)
# using defaults
ggplot(data = d, aes(x, y, label = lab)) +
geom_point() +
stat_dens2d_labels()
ggplot(data = d, aes(x, y, label = lab)) +
geom_point() +
stat_dens2d_labels(keep.these = "zoujdg")
ggplot(data = d, aes(x, y, label = lab)) +
geom_point() +
stat_dens2d_labels(keep.these = function(x) {grepl("^z", x)})
ggplot(data = d, aes(x, y, label = lab)) +
geom_point() +
stat_dens2d_labels(geom = "text_s",
position = position_nudge_center(x = 0.1, y = 0.1,
center_x = mean,
center_y = mean),
vjust = "outward_mean", hjust = "outward_mean") +
expand_limits(x = c(-4, 4.5))
ggrepel.installed <- requireNamespace("ggrepel", quietly = TRUE)
if (ggrepel.installed) {
library(ggrepel)
ggplot(data = d, aes(x, y, label = lab, colour = group)) +
geom_point() +
stat_dens2d_labels(geom = "text_repel")
ggplot(data = d, aes(x, y, label = lab, colour = group)) +
geom_point() +
stat_dens2d_labels(geom = "text_repel", label.fill = NA)
# we keep labels starting with "a" across the whole plot, but all in sparse
# regions. To achieve this we pass as argument to label.fill a fucntion
# instead of a character string.
label.fun <- function(x) {ifelse(grepl("^a", x), x, "")}
ggplot(data = d, aes(x, y, label = lab, colour = group)) +
geom_point() +
stat_dens2d_labels(geom = "text_repel", label.fill = label.fun)
}
# Using geom_debug() we can see that all 100 rows in \code{d} are
# returned. But only those labelled in the previous example still contain
# the original labels.
gginnards.installed <- requireNamespace("gginnards", quietly = TRUE)
if (gginnards.installed) {
library(gginnards)
ggplot(data = d, aes(x, y, label = lab)) +
geom_point() +
stat_dens2d_labels(geom = "debug")
ggplot(data = d, aes(x, y, label = lab)) +
geom_point() +
stat_dens2d_labels(geom = "debug", return.density = TRUE)
ggplot(data = d, aes(x, y, label = lab)) +
geom_point() +
stat_dens2d_labels(geom = "debug", label.fill = NULL)
ggplot(data = d, aes(x, y, label = lab)) +
geom_point() +
stat_dens2d_labels(geom = "debug", label.fill = FALSE, return.density = TRUE)
ggplot(data = d, aes(x, y, label = lab)) +
geom_point() +
stat_dens2d_labels(geom = "debug", label.fill = NULL, return.density = TRUE)
ggplot(data = d, aes(x, y)) +
geom_point() +
stat_dens2d_labels(geom = "debug")
}
Run the code above in your browser using DataLab