0
votes

I have been getting the error Error in default + theme : non-numeric argument to binary operator. I have been using R and teaching R for a long time but I can't find this problem. I have included a reproducible example that fails this way below:

library(tibble)
library(ggplot2)

brains <- as_tibble(brains)
brains <- brains[1:10, ]
brains
ggplot(brains, aes(x = BodyWt, y = BrainWt)) +
  geom_point()

The error occurs when executing the ggplot() statement.

My hardware is an HP Laptop 15-ef0xxx. I am running Windows 10 Home version 2004. I am running RStudio community edition "Water Lily" and R version R x64** 4.0.2.

I know this is a simple error and it is driving me crazy.

2
Hello and welcome to Stack Overflow. Can you please provide us with the data? Without the actual dataset is is hard to debug the problem.itsMeInMiami
I was having this error because I used the %>% instead of the +. I read this thread and tried a bunch of options to correct it but it was a silly waste of time since it was just a syntax error.Andrew Borst
I had this error because of another silly mistake. I was writing theme_set(theme_linedraw) instead of theme_set(theme_linedraw()). Logging this so that maybe it helps someone.Jabro

2 Answers

1
votes

So I finally solved this problem. On the github issue I had opened Hiroaki commented that "One possibility is that you might set a invalid default theme in your .Rprofile, but I'm not sure..." (see link to issue below).

I'm not sure if your R file is part of a project but mine is. So I went back and deleted the theme_set() line in my R file, went in and double checked all my project options and selected the option "Disable .Rprofile execution on session start/resume" and "Quit child processes on exit". And then I restarted the R session and now everything works. Including on the default R editor console. I'm not sure if all those steps are necessary but that seemed to do the trick for me! Hope it helps.

I thought this was an RStudio issue but it seems it's possibly a ggplot2 > problem. I have verified using two different datasets that the same >error comes up when I try using ggplot2 in RStudio or using the default R >console. I get the exact same error with code that's been working fine >but now suddenly won't. I have opened an issue on Github (ggplot2) with a >reprex. Might be worth checking there: >https://github.com/tidyverse/ggplot2/issues/4177

I know this is not an answer per se but I don't have enough reputation >points to add a comment to the previous answer but I thought linking to >the issue on Github might help.

0
votes

I think you have numbers quoted somewhere and you are trying to perform mathematical operation on character values. Consider

  x <- c("5","6")
  y <- x/1

>   y <- x/1
Error in x/1 : non-numeric argument to binary operator

Now try converting x to numeric and perform the same operation.

y <- as.numeric(x)/1
>  y
[1] 5 6

So, you need to use as.numeric on your variable.
The following should resove this issue

ggplot(brains, aes(x = as.numeric(BodyWt), y = as.numeric(BrainWt)))