How to Solve R Error: `data` must be a data frame, or other object coercible by `fortify()`, not a numeric vector

by | Programming, R, Tips

This error occurs when you try to plot the variables from a data frame but specify a numeric vector instead of a data frame for the data argument.

You can solve this error by passing the data frame as the data argument for the ggplot() function call.

This tutorial will go through how to solve the error with code examples.


Table of contents

Example

Let’s look at an example to reproduce the error. First, we will define a data frame containing two numeric vectors.

df <- data.frame(x=c(rnorm(10, mean=10, sd=2)),
                 y=c(rnorm(10, mean=5, sd=3)))

df

Let’s look at an example to reproduce the error. First, we will define a data frame containing two numeric vectors.

           x        y
1  11.452909 2.547714
2  11.427313 7.027680
3   8.699874 4.353557
4  12.997392 4.656051
5   7.128344 4.393204
6   5.677364 6.219478
7  10.790440 6.970317
8   9.210332 5.318572
9   9.380483 4.446808
10 12.661653 7.838102

Next, we will try to create a scatter plot to visualize the two variables x and y in the data frame using ggplot2.

library(ggplot2)

ggplot(df$x, aes(x=x, y=y)) +
    geom_point()

Let’s run the code to see what happens:

Error in `fortify()`:
! `data` must be a data frame, or other object coercible by `fortify()`, not a numeric vector.

The error occurs because we passed a numeric vector df$x as the data argument for the ggplot() function call instead of a data frame.

Solution

We can solve this error by passing the reference to the data frame as the data argument for the ggplot() function call.

Let’s look at the revised code:

library(ggplot2)

ggplot(df, aes(x=x, y=y)) +
    geom_point()

Let’s run the code to get the result:

Scatterplot of random points using ggplot2
Scatterplot of random points

We successfully created the scatterplot without any errors.

Summary

Congratulations on reading to the end of this tutorial!

For further reading on R-related errors, go to the articles:

Go to the online courses page on R to learn more about coding in R for data science and machine learning.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨