Skip to content

Instantly share code, notes, and snippets.

@ateucher
Last active May 30, 2020 19:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ateucher/7953412 to your computer and use it in GitHub Desktop.
Save ateucher/7953412 to your computer and use it in GitHub Desktop.
Manipulating ggplot2 legend with `override.aes` so points only for points, lines only for lines etc.
require(ggplot2)
set.seed(42)
df <- data.frame(x=1:50, y=rnorm(50, 10, 2), int=rbinom(50,1,0.3))
ggplot(df, aes(x=x, y=y)) +
geom_ribbon(aes(ymin=0, ymax=y, fill='#1E90FF'), alpha=0.3) +
geom_abline(intercept=10, slope=-0.1
, aes(colour='Linear')) +
geom_point(data=df[df$int==1,], aes(y=y, colour='Interpolated'), size=2) +
geom_smooth(aes(colour='Smooth'), method='loess') +
theme(panel.background=element_rect(fill='white')) +
scale_fill_identity(name = '', guide = 'legend'
, labels = c('Level')) +
scale_colour_manual(name='', values=c('Interpolated'='red', 'Linear'='orange'
, 'Smooth'='#1E90FF'), guide='legend') +
guides(fill = guide_legend(override.aes = list(linetype = 0, shape=''))
, colour = guide_legend(override.aes = list(linetype=c(0,1,1)
, shape=c(16,NA,NA))))
@peleonard
Copy link

there is an error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment