## add ggplot2
library(ggplot2)
tmp <- data.frame(x=1:50, y=eq(1:50))
# define equations
eq1 <- function(x) { 10 * exp(x * 0.01 ) }
eq1 <- function(x) { 10 * exp(x * 0.02 ) }
dens = stat_function(fun = eq1, color="red")
high = stat_function(fun = eq2, color="green", alpha=I(1/5))
qplot(x, y, data = tmp,
xlab = "x-axis",
ylab = "y-axis",
main = "title"
) + dens + high
thanks.
David
On Feb 10, 9:02 am, daithiquinn <daithiqu...@gmail.com> wrote:
> I'm using the stat_function() command to plot two functions. Is it
> possible to have a legend that describes these functions?
>
> ## add ggplot2
> library(ggplot2)
>
> tmp <- data.frame(x=1:50, y=eq(1:50))
tmp <- data.frame(x=1:50, y=c(1:50)) # function eq not defined
>
> # define equations
> eq1 <- function(x) { 10 * exp(x * 0.01 ) }
> eq1 <- function(x) { 10 * exp(x * 0.02 ) }
eq2 <- function(x) { 10 * exp(x * 0.02 ) } # almost certain you meant
eq2 here
>
> dens = stat_function(fun = eq1, color="red")
> high = stat_function(fun = eq2, color="green", alpha=I(1/5))
dens <- stat_function(fun = eq1, aes(color="red"))
high <- stat_function(fun = eq2, aes(color="green"), alpha=I(1/5))
# in order for there to be a legend, there must be a scale which means
there must be an aesthetic mapping. Thus the colour specifications go
in an aes()
>
> qplot(x, y, data = tmp,
> xlab = "x-axis",
> ylab = "y-axis",
> main = "title"
> ) + dens + high
qplot(x, y, data = tmp,
xlab = "x-axis",
ylab = "y-axis",
main = "title"
) + dens + high +
scale_colour_identity("fits", breaks=c("red","green"),
labels=c("eq1","eq2"))
# defining the colour scale as an identity scale tells it to use the
colors as specified rather than to map two groups to colors in a color
palette.
>
> thanks.
> David
--Brian Diggs