Get Started →
Function Errors

ggplot2 data must be a data frame

Matches: data must be a data\.frame

WHAT THIS ERROR MEANS

ggplot2 requires a data frame (or tibble) as its data argument. You passed a matrix, vector, or other incompatible type.

HOW TO FIX IT

1. Convert your data to a data frame with as.data.frame().

2. Use tibble() or data.frame() to construct the data.

3. Check the type with class(your_data).

CODE EXAMPLES

BAD — THIS CAUSES THE ERROR
mat <- matrix(1:9, nrow = 3)
ggplot(mat, aes(x = V1, y = V2)) + geom_point()
GOOD — CORRECT APPROACH
df <- as.data.frame(matrix(1:9, nrow = 3))
ggplot(df, aes(x = V1, y = V2)) + geom_point()

Still stuck?

Paste your code in RChat and the AI will fix this error in context.

Try RChat Free →

RELATED FUNCTION ERRORS