4

I found and example for the %+% operator in ggplot2 to use a different data frame for the same plot. Examples are available here.

When I try this, I get an error message:

df <- data.frame(x=rnorm(1000), y=rnorm(1000))
p <- ggplot(df, aes(x,y)) + geom_point()
p
p %+% df[1:100,]

Error in p %+% df[1:100, ] : x must be either a vector or a matrix

Did I misunderstood the example on the website?

sessionInfo()

sessionInfo()
R version 3.2.0 (2015-04-16)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.9.5 (Mavericks)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] cfpscripts_0.4.0  seqinr_3.1-3      ade4_1.7-2        gridExtra_0.9.1   psych_1.5.4       reshape2_1.4.1    plyr_1.8.3        knitr_1.10.5      rMQanalysis_0.2.0 ggplot2_1.0.1     testthat_0.10.0  
[12] devtools_1.8.0   

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.0           git2r_0.10.1          tools_3.2.0           digest_0.6.8          memoise_0.2.1         gtable_0.1.2          lattice_0.20-31       curl_0.8              parallel_3.2.0       
[10] proto_0.3-10          stringr_1.0.0         imbproteomicsr_0.1.7  xml2_0.1.1            rversions_1.0.1       data.table_1.9.4      sp_1.1-1              magrittr_1.5          scales_0.2.5         
[19] cfppdfs_0.3.4         MASS_7.3-40           mnormt_1.5-3          splitstackshape_1.4.2 logspline_2.1.8       colorspace_1.2-6      labeling_0.3          stringi_0.5-5         munsell_0.4.2        
[28] chron_2.3-45          crayon_1.3.0          zoo_1.7-12 
6
  • 2
    Works for me on ggplot2_1.0.1. Try updating your ggplot2 package. Also you can see if ?"%+%" finds a ggplot2 function Commented Sep 1, 2015 at 14:06
  • 1
    Just figured out that the psych package overwrites the %+% function. Is there a way to call the ggplot2 version explizit? ggplot2::%+% did not work. Commented Sep 1, 2015 at 14:13
  • 4
    load psych before you load ggplot2? or put single-back-quotes around %+% in `ggplot2:``%+%``` Commented Sep 1, 2015 at 14:14
  • Thanks, that solved my question. Commented Sep 1, 2015 at 14:37
  • @BenBolker I wanted to try the ggplot2: notation since it seems to be more stable in case of errors but I can not get it to work. R always complains about unexpected symbol in "p ggplot2". p is my graph and I try ggplot2::'%+%' but the ' are single back ticks which would now highlight my command. I tried also with only one colon, double back ticks and a lot of different combinations but always the same error. Commented Sep 7, 2015 at 12:33

1 Answer 1

4

The basic problem is loading the psych package after ggplot2, which gives you a message:

Attaching package: ‘psych’ The following objects are masked from ‘package:ggplot2’: %+%, alpha

Since R's parser allows a limited set of characters to delimit an operator, the ::+back-tick trick p ggplot2::`%+%` newdata won't work. Other than making sure to load psych before ggplot2, you could do

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

to create yet another copy of the operator in your global workspace, but it's probably best just to be careful about loading psych and ggplot2.

You could add a test

if(find("%+%")[1]=="package:psych") stop("oops")

to your code to make sure this doesn't happen by accident ...

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

1 Comment

Actually id did the trick with reassigning the %+% to %myadd% within my function, just to make sure... I work a lot with the psych package and it is often loaded in the background and I didn't wanted to bother... I didn't understood why the back tick trick didn't work in my way, but now it is not completely clear ;-) but I got your solution, thanks.

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.