Select Page

# 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.

## 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:

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!

##### Suf
Research Scientist at | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!