Skip to content

Instantly share code, notes, and snippets.

@econandrew
Last active June 21, 2019 17:25
Show Gist options
  • Save econandrew/a986d2f29789238905417c57c1f92825 to your computer and use it in GitHub Desktop.
Save econandrew/a986d2f29789238905417c57c1f92825 to your computer and use it in GitHub Desktop.
# Load data and create base plot
mtcars$names <- rownames(mtcars)
p <- ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(aes(size = disp, color = as.factor(carb))) +
scale_color_brewer(palette="Set1") +
scale_size(range=c(1,20)) +
scale_x_continuous(limits=c(1,4.5)) +
theme_minimal() +
theme(legend.position = "none")
# Single color labelling with unsatisfactory contrast
p + geom_text(data = mtcars %>% filter(mtcars$disp > 380), aes(label=names), hjust=0)
firebird <- mtcars[mtcars$names == "Pontiac Firebird",]
# Simple over-printing for improved contrast
p +
annotate("text", x = firebird$wt, y=firebird$mpg, hjust = 0,
label="Pontiac Firebird", color="black") +
annotate("text", x = firebird$wt, y=firebird$mpg, hjust = 0,
label="Pon", color="white")
# Elaborate over-printing for improved contrast
p +
annotate("text", x = firebird$wt, y=firebird$mpg, hjust = 0, parse=T,
label='"Pon" * phantom("tiac Firebird")', color="white") +
annotate("text", x = firebird$wt, y=firebird$mpg, hjust = 0, parse=T,
label='phantom("Pon") * "tiac Firebird"', color="black")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment