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

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

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?

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()

 

r graph figure 1 must be data frame not s3 object class uneval r

 

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:

 

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.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


4 Comments. Leave new

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top