Skip to content

Instantly share code, notes, and snippets.

@dsparks
Created May 23, 2013 18:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsparks/5638188 to your computer and use it in GitHub Desktop.
Save dsparks/5638188 to your computer and use it in GitHub Desktop.
Hack for white strokes around text
doInstall <- TRUE
toInstall <- c("ggplot2")
if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Make some random data:
randPoints <- data.frame(x = runif(1000), y = runif(1000))
randPoints$color <- hsv(runif(1000), runif(1000), runif(1000))
zp1 <- ggplot(randPoints,
aes(x = x, y = y, colour = color))
zp1 <- zp1 + geom_point(size = 30)
zp1 <- zp1 + scale_color_identity()
print(zp1)
# Make some labels:
labelFrame <- randPoints[1:10, ]
zp2 <- zp1 + geom_text(data = labelFrame,
aes(x = x, y = y, label = color),
colour = "black")
print(zp2)
epsilonX <- diff(ggplot_build(zp1)$panel$ranges[[1]]$x.range) / 500
epsilonY <- diff(ggplot_build(zp1)$panel$ranges[[1]]$y.range) / 500
zp3 <- zp1 + geom_text(data = labelFrame,
aes(x = x+epsilonX, y = y, label = color),
colour = "white", fontface = "bold")
zp3 <- zp3 + geom_text(data = labelFrame,
aes(x = x-epsilonX, y = y, label = color),
colour = "white", fontface = "bold")
zp3 <- zp3 + geom_text(data = labelFrame,
aes(x = x, y = y+epsilonY, label = color),
colour = "white", fontface = "bold")
zp3 <- zp3 + geom_text(data = labelFrame,
aes(x = x, y = y-epsilonY, label = color),
colour = "white", fontface = "bold")
zp3 <- zp3 + geom_text(data = labelFrame,
aes(x = x, y = y, label = color),
colour = "black")
print(zp3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment