Last active
May 30, 2020 19:41
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
there is an error