Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.