error in geom_smooth

418 views
Skip to first unread message

Elena

unread,
Jun 7, 2011, 6:19:22 PM6/7/11
to ggplot2
Hi,

I've had a lot of trouble adding a smooth geom to my scatterplots.
I've searched through the ggplot2 archive for the answer, but unable
to find one, I thought I'd appeal to the infinite wisdom of the
ggplot2 listserv subscribers. I've already tried method = "gam" from
the mgcv library, which is meant for modelling large datasets. My code
is below.

Thanks in advance,
Elena

#first I took a subset of my data, encompassing 10,000 rows
> small <- Fig1A[sample(nrow(Fig1A), 10000),]
> b <- ggplot(data=small, aes(X.val, Y.val)) + scale_x_log10() + scale_y_log10() + geom_point()
> b
#this appears to work great, but then I run into a problem when I try
to add geom_smooth, as implemented on p. 15 of the ggplot2 book when
working with large dataframes
> b + geom_smooth(method = "gam", formula = y ~ s(x, bs = "cs"), se = TRUE, level = 0.99, alpha = 1, span =0.2)
Error in smooth.construct.cr.smooth.spec(object, data, knots) :
NA/NaN/Inf in foreign function call (arg 1)
#the default also doesn't work:
> b + geom_smooth()
Error in smooth.construct.cr.smooth.spec(object, data, knots) :
NA/NaN/Inf in foreign function call (arg 1)
> sessionInfo()
R version 2.13.0 (2011-04-13)
Platform: i386-pc-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] splines grid stats graphics grDevices utils
datasets
[8] methods base

other attached packages:
[1] foreign_0.8-43 arm_1.4-11 abind_1.3-0
[4] R2WinBUGS_2.1-18 coda_0.14-4 lme4_0.999375-39
[7] Matrix_0.999375-50 lattice_0.19-23 MASS_7.3-12
[10] Hmisc_3.8-3 survival_2.36-5 mgcv_1.7-5
[13] ggplot2_0.8.9 proto_0.3-9.2 reshape_0.8.4
[16] plyr_1.5.2

Joshua Wiley

unread,
Jun 7, 2011, 6:41:48 PM6/7/11
to Elena, ggplot2
What about precomputing the smooth and just using ggplot2 to draw the line?

Josh

> --
> 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
>

--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/

Dennis Murphy

unread,
Jun 7, 2011, 7:49:16 PM6/7/11
to Elena, ggplot2
Hi:

It's possible that you have a sparsity of data points in a certain
region (usually on one end or the other of the x-axis) that causes the
smoother to choke. A span of 0.2 may be a bit tight - the default in
loess() is 0.75, for example. You might try increasing the span to 0.3
or 0.4 to see if that helps - it will likely create a smoother curve
than the model with span 0.2.

Untested since no referent data was provided, so caveat emptor...

HTH,
Dennis

On Tue, Jun 7, 2011 at 3:19 PM, Elena <elena....@gmail.com> wrote:

Joran

unread,
Jun 7, 2011, 8:02:21 PM6/7/11
to ggplot2
I suspect Dennis may be right, but another thing to check is whether
you have NAs in your data and that you haven't set the NA action to
na.fail or something.
Reply all
Reply to author
Forward
0 new messages