aes should be used to tell ggplot which variables in a data frame are to
be displayed as which aesthetics; vectors should not be given to them.
What is happening is that it is picking up x from the data d (not the
variable) and y as, well, I'm not exactly sure what. The last line
should be:
p1 + geom_line(aes(x,y), data=data.frame(x,y=predict(m2,list(xv=x))),
colour="red")
> I am confused about this error. Could someone help me? Thank you so
> much!
--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University
It isn't totally clear to me what exactly you are trying to do. But if
you are trying to plot a line based on a linear model fit then I think
this is what you are after:
library(ggplot2)
d=read.table("http://www.bio.ic.ac.uk/research/mjcraw/therbook/data/diminish.txt",header=TRUE)
ggplot(d, aes(x=xv, y=yv)) +
geom_point() +
stat_smooth(method="lm", se=FALSE)
Does that help?
Sam
> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
geom_smooth() makes this very easy:
qplot(xv, yv, data = d) +
geom_smooth(method = 'lm', color = 'blue', se = FALSE) +
geom_smooth(method = 'lm', formula = y ~ poly(x, 2), color = 'red',
se = FALSE)
geom_smooth() expects a generic y and x in the formula - it will pick
up the variables corresponding to x and y from qplot(). BTW,
geom_smooth() actually does something very similar internally to what
your code is attempting to produce, except that it fits the function
to 80 points between the min and max rather than 100.
To more or less reproduce the plot you're trying to construct, I'd
proceed as follows:
# This is the input to the newdata = argument of predict(),
# which expects a data frame:
px <- data.frame(xv = seq(min(d$xv), max(d$xv), length = 100))
# Create a new data frame with the prediction points, the
# linear and quadratic fitted values, respectively
pd <- data.frame(px, yhat1 = predict(m1, newdata = px),
yhat2 = predict(m2, newdata = px))
p + geom_line(data = pd, aes(x = xv, y = yhat1), color = 'blue') +
geom_line(data = pd, aes(x = xv, y = yhat2), color = 'red')
## This plot ^ will not produce a legend to distinguish the
## colors of the curves and should closely resemble the first plot.
# Do this to set up a legend for the fits:
# First, melt the data using xv as the 'id' variable
pm <- reshape2::melt(pd, id = 'xv')
# Next, create a new factor named model with labels 'linear'
# and 'quadratic', and use this to create the color legend:
pm$model <- pm$variable
pm$model <- factor(pm$model, labels = c('linear', 'quadratic'))
# Do the plot and notice the legend title and labels:
p + geom_line(data = pm, aes(x=xv, y = value, color= model)) +
labs(y = 'yv')
ggplot2 is being actively developed, so code that is about four years
old has a fairly high probability of breaking in recent versions of
ggplot2, and possibly in recent versions of R. If you're trying to
follow Crawley using ggplot2, I'd suggest consulting the on-line help
pages at http://had.co.nz/ggplot2/ (scroll toward the bottom of the
page to find them) for code specific to ggplot2.
HTH,
Dennis
On Fri, Apr 6, 2012 at 6:51 AM, Debbie Smith <dsmit...@gmail.com> wrote:
Actually, x does not exist in the data frame d, only in the general environment. When a variable does not exist in the data source, ggplot fetches it in the environment, so it should work (the result of predict has 100 elements, as does x, so there is no size mismatch here). Apparently it does not work when all aesthetics come from the environment and a data frame is still inherited from the previous layers of the plot. Here the data is inherited from the first qplot call. Indeed, this works:
ggplot() + geom_point(aes(xv, yv), data=d) + geom_abline(intercept=coefficients(m1)[1],slope=coefficients(m1)[2]) + geom_line(aes(x=x,y=predict(m2,list(xv=x))), color="red")
but this does not
ggplot(d) + geom_point(aes(xv, yv), data=d) + geom_abline(intercept=coefficients(m1)[1],slope=coefficients(m1)[2]) + geom_line(aes(x=x,y=predict(m2,list(xv=x))), color="red")
and produces the same error
Error in data.frame(evaled, PANEL = data$PANEL) :
arguments imply differing number of rows: 100, 18
I don't know wether this is worth a bug report. In any case, it shows that, for maximum flexibility you should always use the ggplot() + geom_…(…) syntax, with a full specification in each geom.
Jean-Olivier Irisson
---
Observatoire Océanologique
Station Zoologique, B.P. 28, Chemin du Lazaret
06230 Villefranche-sur-Mer
Tel: +33 04 93 76 38 04
Mob: +33 06 21 05 19 90
http://jo.irisson.com/
You received this message because you are subscribed to a topic in the Google Groups "ggplot2" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ggplot2/sB9TyBvySkQ/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to ggplot2+u...@googlegroups.com.