9

I'd like to create a custom Stat object for ggplot2. (Specifically I'd like to create a smoother that works differently than the ones stat_smooth allows- for instance, without a y~x modeling function- but there are other custom Stats I'd like to create even if there were a workaround for my specific case).

I found this suggested solution from Hadley Wickham:

StatExpo <- proto(Stat, {
  objname <- "expo"
  desc <- "Exponential smoothing"
  default_geom <- function(.) GeomLine

  calculate_groups <- function(., data, scales, variable="x", ...) {
    data$y <- HoltWinters(data$x, ...)
  }
})
stat_expo <- StatExpo$new

However, when I try it I get:

Error in proto(Stat, { : object 'Stat' not found

Upon looking around the ggplot code, I found where Stat is defined. However, the Stat object is, as far as I can tell, never exported from ggplot2.

I could write my new stat object within the ggplot2/R folder and then reinstall the package, but obviously this would be cumbersome and make the solution very difficult to share with others. How can I create a custom Stat object outside of the ggplot namespace?

4
  • does ggplot2:::Stat help? Commented Aug 7, 2013 at 16:34
  • @baptiste Nope, Error: 'Stat' is not an exported object from 'namespace:ggplot2'. Does it work for you? Commented Aug 7, 2013 at 16:34
  • it works with three ::: Commented Aug 7, 2013 at 16:38
  • @baptiste Ah! I misread. That works; please post it as an answer. Commented Aug 7, 2013 at 16:39

2 Answers 2

5

ggplot2:::Stat can be used to access the non-exported object.

Sign up to request clarification or add additional context in comments.

1 Comment

Needing ::: will prevent a user-defined function from being included in a package to be submitted to CRAN, unfortunately. CRAN checks do not allow :::.
1
getFromNamespace('Stat','ggplot2')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.