ggplot2: use% +% para trazar nuevos datos

I'm hitting a snag when I try to use the %+% operator to redo an existing plot with new data. My code looks like this:

df <- data.frame(ending=now()+hours(0:5), actual=runif(6), pred=runif(6))
p <- ggplot(df, aes(x=ending)) +
  geom_line(aes(y=actual, color='Actual')) +
  geom_line(aes(y=pred, color='Predicted')) +
  ylab('Faults') +
  scale_color_manual('Values', c("Predicted"="red", "Actual"="black"))

That works fine. But when I try to substitute a new df, I hit errors:

p1 %+% df
Error in bl1$get_call : $ operator is invalid for atomic vectors

¿Alguna idea?

preguntado el 09 de marzo de 12 a las 23:03

2 Respuestas

Of course, immediately after I post, I find the answer - it's not ggplot2's %+% operator. Another namespace collision. The mboost package also provides a %+% operador.

I "solved" this by doing detach(package:mboost). I could also solve it by doing something like

replot <- get('%+%', 'package:ggplot2')
replot(p, df)

A solution to avoiding the namespace collision would be best, but I don't know how to do that.

respondido 09 mar '12, 23:03

Lo mejor que se me ocurre es ggplot2::`%+%`(p, df), but I don't know how to combine specifying a infix binary operator and a namespace qualification. - Brian Diggs

Can infix binary operators be methods? That might help solve the namespace collision, maybe, possibly. - Ken Williams

If you don't use mboost's %+%, you can add `%+%` <- ggplot2::`%+%` to your RProfile to permanently alias it. - leonel henry

You can reassign infix operators to infix operators, but I don't think you can then turn them back into regular functions without special effort. Try this instead:

 `%new+%` <- ggplot2::`%+%`

.... and use it as p %+% df, en lugar de como %+%(a,b)

respondido 10 mar '12, 14:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.