ggplot2 Error in R: Must be Data Frame not S3 Object with Class Uneval
In this article you’ll learn how to handle the ggplot2 error message “data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class uneval” in the R programming language.
The article is structured as follows:
Let’s dig in!
Example Data, Packages & Default Plot
First, let’s construct some example data in R:
data <- data.frame(x = 4:9, # Create example data y = 1:6) data # Print example data # x y # 1 4 1 # 2 5 2 # 3 6 3 # 4 7 4 # 5 8 5 # 6 9 6 |
data <- data.frame(x = 4:9, # Create example data y = 1:6) data # Print example data # x y # 1 4 1 # 2 5 2 # 3 6 3 # 4 7 4 # 5 8 5 # 6 9 6
The previous output of the RStudio console shows the structure of our example data: It consists of six rows and two numeric columns.
If we want to use the functions of the ggplot2 package, we also have to install and load ggplot2:
install.packages("ggplot2") # Install ggplot2 package library("ggplot2") # Load ggplot2 package |
install.packages("ggplot2") # Install ggplot2 package library("ggplot2") # Load ggplot2 package
That’s it with the preparation. Let’s move on the examples!
Example 1: Reproduce the Error: `data` must be a data frame, or other object coercible by `fortify()`
The following R programming code shows how to replicate the error “data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class uneval” in R.
Have a look at the following ggplot2 syntax:
ggplot(aes(data, x, y)) + # Try to draw ggplot2 plot geom_point() # Error: `data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class uneval # Did you accidentally pass `aes()` to the `data` argument? |
ggplot(aes(data, x, y)) + # Try to draw ggplot2 plot geom_point() # Error: `data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class uneval # Did you accidentally pass `aes()` to the `data` argument?
As you can see, the error message “data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class uneval” was returned by the RStudio console.
The reason for this is that we have specified our data at the wrong position, i.e. within the aes function.
So how can we solve this problem? Keep on reading!
Example 2: Fix the Error: `data` must be a data frame, or other object coercible by `fortify()`
In Example 2, I’ll show how to fix the error message “data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class uneval”.
For this, we have to specify our data outside of the aes function:
ggplot(data, aes(x, y)) + # Properly draw ggplot2 plot geom_point() |
ggplot(data, aes(x, y)) + # Properly draw ggplot2 plot geom_point()
The output of the previous R syntax is shown in Figure 1 – Looks good!
Video & Further Resources
Do you want to know more about errors and warnings in the R programming language? Then you might want to watch the following video of my YouTube channel. I illustrate the topics of this tutorial in the video.
The YouTube video will be added soon.
Besides that, you could have a look at the other tutorials which I have published on my website. You can find some articles below:
- ggplot2 Error: Aesthetics must be either length 1 or the same as the data
- ggplot2 Error: stat_count() must not be used with a y aesthetic
- Introduction to R
To summarize: You learned in this article how to deal with the error “data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class uneval” in the R programming language. Please let me know in the comments, if you have any additional questions.
2 Comments. Leave new
Thank you so much, it solved my problem!
Thank you yasaman, glad to hear that it helped!