Error in FUN(X[[i]] …) object X not found, Reproducing and Debugging the “Error in FUN(X[[i]], …) : object not found” when Using geom_path in ggplot2

When working with the geom_path function in ggplot2, it’s not uncommon to encounter the error “Error in FUN(X[[i]], …) : object not found”.

This error typically occurs when the geom_path function cannot find a specific object or variable, usually the ‘group’ variable, within the data set being plotted.

This tutorial will explore how to reproduce and debug this error using a practical example.

### Example Data, Add-On Packages & Default Plot

To begin, we’ll create an example data frame that will serve as the basis for our tutorial. The data frame contains six rows and three columns, with variables x, y, and group.

``````data <- data.frame(x = 1:6,
y = 1:3,
group = rep(letters[1:3], each = 2))
data``````

We’ll also install and load the ggplot2 package to enable us to use its functions.

``````install.packages("ggplot2")
library("ggplot2")``````

Next, we’ll create a default ggplot plot using the ggplot function without specifying a path.

``````ggp <- ggplot(data,
aes(x = x,
y = y,
col = group)) +
geom_point()
ggp``````

This will produce a scatterplot with points colored according to the group variable. We can see that the plot is successfully generated without any errors.

Descriptive Statistics in R » Data Science Tutorials

### Specifying Path Coordinates

Now, let’s create a new data frame containing our path coordinates.

``````data_path <- data.frame(x_path = c(1, 3, 5),
y_path = c(3, 2, 3))
data_path``````

This new data frame contains the coordinates for a path that we’d like to add to our original plot.

### Adding Path to Plot (Example 1)

Let’s attempt to add this path to our original plot using the geom_path function.

``````ggp +
geom_path(data = data_path,
aes(x = x_path,
y = y_path))

As expected, this attempt results in an error message “Error in FUN(X[[i]], …) : object ‘group’ not found”. This is because the geom_path function is searching for the group variable in our path data set, but it’s not present.

### Debugging the Error (Example 2)

To resolve this issue, we can set the inherit.aes argument within the geom_path function to FALSE. This tells ggplot not to inherit any aesthetics from the parent layer.

``````ggp +
geom_path(data = data_path,
aes(x = x_path,
y = y_path),
inherit.aes = FALSE)``````

By setting inherit.aes to FALSE, we’re able to add the path to our original plot without encountering any errors. The resulting plot includes both the points from our original data and the specified path.

## Conclusion

In this tutorial, we’ve explored how to reproduce and debug the “Error in FUN(X[[i]], …) : object not found” when using geom_path in ggplot2.

By understanding why this error occurs and how to resolve it, you’ll be better equipped to create complex and customized plots using ggplot2.

Remember to always carefully consider your data and aesthetics when working with this powerful visualization tool.

What Data Science Is and What You Can Do With It » finnstats