Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error: Have you misspelled the `data` argument in `ggplot()`

I would like to simply visualize a plot in Quarto but keep getting an error which I don’t understand. When I run my qmd file I get an error in this chunk:

ggplot(df, aes(x = X, y = Y)) +
  geom_bin2d(binwidth = c(5, 5)) +
  stat_bin2d(geom = "text", aes(label = ..count..), size = 2, binwidth = c(5, 5)) +
  scale_fill_gradient("Aantal schoten", low = "yellow", high = "red") +
  annotate_pitch(alpha = 0) +
  direction_label(x_label = 50) +
  theme_pitch() +
  theme(legend.key=element_blank()) 

Error:

Error in `ggplot()`:
! `data` cannot be a function.
ℹ Have you misspelled the `data` argument in `ggplot()`
Backtrace:
 1. ggplot2::ggplot(df, aes(x = X, y = Y))
 2. ggplot2:::ggplot.function(df, aes(x = X, y = Y))
                                                                                                             
Execution halted

I checked my global environment and there is no variable called data which may be the problem. When I run a simple similar file there is no error like this one:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

---
title: "test"
---

```{r}
library(ggplot2)
library(dplyr)
iris %>%
  ggplot(aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point()
```

So I don’t understand why this error keeps happening. I also tried using data = df in the ggplot function but this doesn’t work. Also when running the code chunk by chunk does work. The error only happens when rendering the file. So does anyone know why this error happens?

>Solution :

It’s likely the actual name of your data.frame isn’t df, or you created a df variable in your global environment but not your Quarto environment.

Note, it’s not a great idea to use df as the name of a data.frame because df() is a built in function in R for the density of the F-distribution (hence the error about using a function).

If the code only errors when compiling, you likely didn’t import your data in the Quarto document itself. Documents are rendered in a new, fresh environment; so any data in your global environment is not available to the document. You need to make sure your Quarto document is self-sufficient; that is, make sure the read.table or equivalent function to load your data is included in the .qmd file.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading