[R] nls, lattice, and conversion over to ggplot

hadley wickham h.wickham at gmail.com
Thu Oct 9 21:45:54 CEST 2008

On Thu, Oct 9, 2008 at 2:29 PM, stephen sefick <ssefick at gmail.com> wrote:
> I am trying to figure out how to use ggplot2.  I would like to do the below
> with ggplot, but I can not figure out how.  The data provided is a subset of
> a much larger data set, but these data are the data necessary to make the
> plot.  I think I would rather have the colors become symbols, and I do know
> how to do that in lattice, but here is a quick and dirty version.
> thanks

Here's one way:

pred <- data.frame(GPP = f, TSS = y)
qplot(TSS, GPP, data=r, colour=RiverMile) +
geom_line(data=pred, colour ="black")

 * ggplot2 works with data frames, so the key is to create one from
your model predictions.  Naming the variables to match the names of
the model inputs makes sense, and saves some typing.

 *  You no longer need to specify xlim because ggplot2 knows about
everything you are plotting and can calculate the limits

 * You have have to manually set the colour in geom_line to override
the default mapping that you created between colour and RiverMile.



More information about the R-help mailing list