r - ggplot2: legends for different aesthetics -


i first plot histogram group of simulated data , fill bars 1 colour. add line of density function data simulated , make line different colour. want use legends show 1 colour (the fill colour of histogram) samples whereas other (the colour of line) theoretical density. how can achieve this?

enter image description here

the code follows

require(ggplot2) df <- data.frame(x=rnorm(10^4)) p <- ggplot(df, aes(x=x)) + geom_histogram(aes(y=..density..), fill='steelblue', colour='black', alpha=0.8, width=0.2) x <- seq(-4, 4, 0.01) df <- data.frame(x=x, y=dnorm(x)) p <- p + geom_line(data=df, aes(x=x, y=y), colour='red', size=1.5) p 

without changing data @ all, can specify literal aes() values can define later via manual scales.

df <- data.frame(x=rnorm(10^4)) p <- ggplot(df, aes(x=x)) + geom_histogram(aes(y=..density.., fill="samples"),      alpha=0.8, colour="black", width=0.2) p <- p+scale_fill_manual("",breaks="samples", values="steelblue")  x <- seq(-4, 4, 0.01) df <- data.frame(x=x, y=dnorm(x)) p <- p + geom_line(data=df, aes(x=x, y=y, colour="theory"), size=1.5) p <- p+scale_color_manual("",breaks="theory", values="red") 

enter image description here


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -