Plotting a log-curve from a subset

26 views
Skip to first unread message

Maria Petrova

unread,
May 6, 2014, 1:42:36 PM5/6/14
to ggp...@googlegroups.com
Hello,

I want to plot points from one data set and fit a log-curve from a subset. Here is my reproducible example with the errors I encounter:

data <- structure(list(FireRotation = c(4.4, 2.8, 3.3, 3.6, 4.7, 2.6, 
2.4, 2.5, 4.6, 5.3, 3.1, 4.8, 4.1, 6.5, 7.1, 5.8, 7.7, 5.4, 6.7, 
3.6, 3.3, 3.6), herb = c(16.4775, 0.304, 4.849, 1.4965, 2.6715, 
10.834, 10.8915, 13.1245, 4.678, 3.41, 6.8, 3.713, 9.7355, 0.38, 
4.224, 2.917, 7.9925, 6.213, 1.535, 2.4055, 5.0555, 3.6555)), .Names = c("FireRotation", 
"herb"), class = "data.frame", row.names = c(NA, -22L)) 

# the subset
maxPairs <- structure(list(FireRotation = c(2.5, 3.1, 4.4, 5.4, 6.7, 7.7), 
    herb = c(13.1245, 6.8, 16.4775, 6.213, 1.535, 7.9925)), .Names = c("FireRotation", 
"herb"), row.names = c(8L, 11L, 1L, 18L, 19L, 17L), class = "data.frame")

p <- ggplot(data, aes(FireRotation, herb )) + geom_point()
p + stat_smooth( method="lm", data = maxPairs, formula = herb ~ log(FireRotation) )
#Error in eval(expr, envir, enclos) : object 'herb' not found

#OR trying explicitly
p + stat_smooth( method="lm", data = maxPairs, formula = maxPairs$herb ~ log(maxPairs$FireRotation) )
#Error in data.frame(x = xseq, fit, se = pred$se) : 
#  arguments imply differing number of rows: 80, 6
#In addition: Warning message:
#'newdata' had 80 rows but variables found have 6 rows 

May be I should not be using stat_smooth. As you can see below I got the plot to work with the basic graphing package but I know it will look so much better with gglot. 
Thank you in advance!
Inline image 1 

Brandon Hurr

unread,
May 6, 2014, 1:55:51 PM5/6/14
to Maria Petrova, ggplot2
I've not done much with stat_smooth, but it looks like rather than specify the formula using the variables, you specify it using x and y. 
The default is y~x. 

If you specify y~log(x) it seems to work. 

ggplot(data, aes(FireRotation, herb )) + geom_point() + stat_smooth(method="lm", data = maxPairs, formula=y~log(x))

HTH, 

Brandon

Ito, Kaori (Groton)

unread,
May 6, 2014, 1:56:20 PM5/6/14
to Maria Petrova, ggp...@googlegroups.com

Are you looking for something like this?

 

p <- ggplot(data, aes(FireRotation, herb)) + geom_point()

p + stat_smooth(method="lm", data = maxPairs, formula = y ~ log(x), se = FALSE, colour="black")

 

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

Maria Petrova

unread,
May 6, 2014, 2:49:50 PM5/6/14
to ggp...@googlegroups.com
Got it! Works great!
Thanks again!
Reply all
Reply to author
Forward
0 new messages