p <- ggplot(mtcars, aes(wt, mpg, label = rownames(mtcars)))
# Avoid overlaps by repelling text labels
p + geom_text_repel()
# Labels with background
p + geom_label_repel()
## Not run:
# p + geom_text_repel(family = "Times New Roman",
# box.padding = unit(0.5, "lines"))
#
# # Add aesthetic mappings
# p + geom_text_repel(aes(colour = factor(cyl)))
# p + geom_label_repel(aes(fill = factor(cyl)), colour = "white", fontface = "bold")
#
# # 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 = unit(1, "lines"))
# ## End(Not run)
Run the code above in your browser using DataLab