# NOT RUN {
my.cars <- mtcars[c(TRUE, FALSE, FALSE, FALSE), ]
my.cars$name <- rownames(my.cars)
p <- ggplot(my.cars, aes(wt, mpg, label = name))
# default behavior is as for geon_text()
p + geom_linked_text()
# Avoid overlaps
p + geom_linked_text(check_overlap = TRUE)
# Change size of the label
p + geom_linked_text(size = 2.5)
# Use nudging
p +
geom_point() +
geom_linked_text(hjust = -0.04, nudge_x = 0.12) +
expand_limits(x = 6.2)
p +
geom_point() +
geom_linked_text(hjust = -0.04, nudge_x = 0.12,
arrow = arrow(length = grid::unit(1.5, "mm"))) +
expand_limits(x = 6.2)
p +
geom_point() +
geom_linked_text(vjust = -0.5, nudge_y = 0.5)
p +
geom_point() +
geom_linked_text(hjust = -0.02, nudge_x = 0.1,
vjust = -0.2, nudge_y = 0.5)
p +
geom_point() +
geom_linked_text(angle = 90,
hjust = -0.04, nudge_y = 1,
arrow = arrow(length = grid::unit(1.5, "mm"))) +
expand_limits(y = 40)
# Add aesthetic mappings
p +
geom_point() +
geom_linked_text(aes(colour = factor(cyl)),
angle = 90,
hjust = -0.04, nudge_y = 1,
arrow = arrow(length = grid::unit(1.5, "mm"))) +
scale_colour_discrete(l = 40) +
expand_limits(y = 40)
p + geom_linked_text(aes(size = wt)) +
expand_limits(x = c(2, 6))
# Scale height of text, rather than sqrt(height)
p +
geom_linked_text(aes(size = wt)) +
scale_radius(range = c(3,6)) +
expand_limits(x = c(2, 6))
# You can display expressions by setting parse = TRUE. The
# details of the display are described in ?plotmath, but note that
# geom_linked_text uses strings, not expressions.
p +
geom_linked_text(
aes(label = paste(wt, "^(", cyl, ")", sep = "")),
parse = TRUE
)
# Add a text annotation
p +
geom_linked_text() +
annotate(
"linked_text", label = "plot mpg vs. wt",
x = 2, y = 15, size = 3, colour = "red"
) +
expand_limits(x = c(1.5, 6))
# Justification -------------------------------------------------------------
df <- data.frame(
x = c(1, 1, 2, 2, 1.5),
y = c(1, 2, 1, 2, 1.5),
text = c("bottom-left", "bottom-right", "top-left", "top-right", "center")
)
ggplot(df, aes(x, y)) +
geom_linked_text(aes(label = text))
ggplot(df, aes(x, y)) +
geom_linked_text(aes(label = text), vjust = "inward", hjust = "inward")
# }
Run the code above in your browser using DataLab