p <- ggplot(mtcars,
aes(wt, mpg, label = rownames(mtcars), colour = factor(cyl))) +
geom_point()
# Avoid overlaps by repelling text labels
p + geom_text_repel()
# Labels with background
p + geom_label_repel()
if (FALSE) {
p + geom_text_repel(family = "Times New Roman",
box.padding = 0.5)
# Add aesthetic mappings
p + geom_text_repel(aes(alpha=wt, size=mpg))
p + geom_label_repel(aes(fill=factor(cyl)), colour="white", segment.colour="black")
# Draw all line segments
p + geom_text_repel(min.segment.length = 0)
# Omit short line segments (default behavior)
p + geom_text_repel(min.segment.length = 0.5)
# Omit all line segments
p + geom_text_repel(segment.colour = NA)
# Repel just the labels and totally ignore the data points
p + geom_text_repel(point.size = NA)
# Hide some of the labels, but repel from all data points
mtcars$label <- rownames(mtcars)
mtcars$label[1:15] <- ""
p + geom_text_repel(data = mtcars, aes(wt, mpg, label = label))
# Nudge the starting positions
p + geom_text_repel(nudge_x = ifelse(mtcars$cyl == 6, 1, 0),
nudge_y = ifelse(mtcars$cyl == 6, 8, 0))
# Change the text size
p + geom_text_repel(aes(size = wt))
# Scale height of text, rather than sqrt(height)
p + geom_text_repel(aes(size = wt)) + scale_radius(range = c(3,6))
# You can display expressions by setting parse = TRUE. The
# details of the display are described in ?plotmath, but note that
# geom_text_repel uses strings, not expressions.
p + geom_text_repel(aes(label = paste(wt, "^(", cyl, ")", sep = "")),
parse = TRUE)
# Add a text annotation
p +
geom_text_repel() +
annotate(
"text", label = "plot mpg vs. wt",
x = 2, y = 15, size = 8, colour = "red"
)
# Add arrows
p +
geom_point(colour = "red") +
geom_text_repel(
arrow = arrow(length = unit(0.02, "npc")),
box.padding = 1
)
}
Run the code above in your browser using DataLab