Fitting a logarithmic curve to a ggplot

5,564 views
Skip to first unread message

James Curley

unread,
Mar 17, 2014, 1:57:02 PM3/17/14
to ggplot2
Hi everyone,

I'm having lots of difficulty with this situation and cannot find the
solution elsewhere.

Example data:

newdf <- data.frame(x=c(3, 1, 10, 8, 63, 8, 82, 38, 6, 4, 7, 4),
y=c(1, 2.05, 3.4, 1.9, 3.2, 3.3, 3.4, 3.5, 2.5, 1.8, 2, 1.8))
newdf
ggplot(newdf, aes(x=x, y=y)) + geom_point()


I'm trying to plot a logarithmic curve to these data. Does anyone
know if this is possible in ggplot?


Thanks

James Curley

Ben Bolker

unread,
Mar 17, 2014, 3:39:58 PM3/17/14
to James Curley, ggplot2
On 14-03-17 01:57 PM, James Curley wrote:
> Hi everyone,
>
> I'm having lots of difficulty with this situation and cannot find the
> solution elsewhere.
>
> Example data:
[rearranged]

>
> I'm trying to plot a logarithmic curve to these data. Does anyone
> know if this is possible in ggplot?
>

You could do this:

newdf <- data.frame(x=c(3, 1, 10, 8, 63, 8, 82, 38, 6, 4, 7, 4),
y=c(1, 2.05, 3.4, 1.9, 3.2, 3.3, 3.4, 3.5, 2.5, 1.8, 2, 1.8))
library(ggplot2)
(g0 <- ggplot(newdf, aes(x=x, y=y)) + geom_point())
g0 + geom_smooth(method="lm",formula=y~log(x))

That fits the model Y ~ N(a+b*log(x),sigma^2)
which may or may not be what you want (but which looks
reasonable in this case)

Dennis Murphy

unread,
Mar 17, 2014, 8:19:14 PM3/17/14
to James Curley, ggplot2
In addition to Ben's reply, you have a few other options:

# Option 1: Transform x in the ggplot() call; the x-axis
# will then be in the log scale
ggplot(newdf, aes(x = log(x), y = y)) + geom_point() +
geom_smooth(method = lm)

# Option 2: Use the trans = option of scale_x_continuous()
ggplot(newdf, aes(x = x, y = y)) + geom_point() +
geom_smooth(method = lm) +
scale_x_continuous(trans = "log")

In options 1 and 2, both transform x to the log scale before computing
the axes. The difference between the two is the set of default breaks;
you can use the breaks = argument in scale_x_continuous() to define
your own.

Option 3 applies a coordinate transformation, which occurs after the
original scales have been drawn. The effect is to warp the original
scaling on the x-axis to a logarithmic one as well as the shape of the
fitted model. You can use this to see the connection between Ben's
solution and the two given above:

ggplot(newdf, aes(x = x, y = y)) + geom_point() +
geom_smooth(method = lm, formula = y ~ log(x)) +
coord_trans(xtrans = "log")

Dennis
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> --- You received this message because you are subscribed to the Google
> Groups "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages