Ggplot2: use% +% to create new data

I am trying to use a statement %+%to redo an existing chart with new data. My code is as follows:

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"))
p

It works great. But when I try to replace a new one df, I find errors:

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

Any thoughts?

+5
source share
2 answers

Of course, immediately after publication, I find the answer - this is not an operator ggplot2 %+%. Another namespace clash. The package is mboostalso provided by the operator %+%.

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

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

A solution to avoid namespace clashes would be better, but I don't know how to do it.

+4
source

infix , , . :

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

.... p %+% df, %+%(a,b)

+1

All Articles