Table of Contents
GGPlot2 is a powerful data visualization package in R that is capable of handling various types of data. Among these is the class “uneval”, which refers to unevaluated expressions or objects in R. This means that GGPlot2 is able to process and display data in its unevaluated state, allowing for a more flexible and dynamic approach to data analysis and visualization. This feature makes GGPlot2 a versatile tool for working with complex and diverse datasets.
Fix: ggplot2 doesn’t know how to deal with data of class uneval
One error you may encounter in R is:
Error: ggplot2 doesn't know how to deal with data of class uneval
This error usually occurs when you attempt to use ggplot2 to plot two data frames at once, but fail to use the data argument within the geom_line() function.
This tutorial shares exactly how to fix this error.
How to Reproduce the Error
Suppose we have two data frames in R that show the number of sales made during specific hours on various days:
#create first data frame
df <- data.frame(date=c(1, 1, 1, 2, 2, 2, 3, 3, 3),
hour=c(1, 2, 3, 1, 2, 3, 1, 2, 3),
sales=c(2, 5, 7, 5, 8, 12, 10, 14, 13))
#view data frame
head(df)
date hour sales
1 1 1 2
2 1 2 5
3 1 3 7
4 2 1 5
5 2 2 8
6 2 3 12
#create second data frame
df_new <- data.frame(date=c(4, 4, 4, 5, 5, 5),
hour=c(1, 2, 3, 1, 2, 3),
sales=c(12, 13, 19, 15, 18, 20))
#view data frame
head(df_new)
date hour sales
1 4 1 12
2 4 2 13
3 4 3 19
4 5 1 15
5 5 2 18
6 5 3 20Now suppose we attempt to create a line chart to visualize the sales grouped by day and hour, using the color blue for the first data frame and red for the second data frame:
library(ggplot2)
#attempt to create line chart
ggplot(df, aes(x=hour, y=sales, group=date)) +
geom_line(color='blue') +
geom_line(df_new, aes(x=hour, y=sales, group=date), color='red')
Error: ggplot2 doesn't know how to deal with data of class uneval
We receive an error because we failed to use the data argument within the second geom_line() function.
How to Fix the Error
The way to fix this error is to simply type out data within the second geom_line() argument so that R knows which data frame we’re attempting to plot.
library(ggplot2)
#create line chart
ggplot(df, aes(x=hour, y=sales, group=date)) +
geom_line(color='blue') +
geom_line(data=df_new, aes(x=hour, y=sales, group=date), color='red')
Notice that we’re able to create the line chart successfully without any error this time.
Additional Resources
The following tutorials explain how to troubleshoot other common errors in R:
Cite this article
stats writer (2024). Can ggplot2 handle data of class uneval?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/can-ggplot2-handle-data-of-class-uneval/
stats writer. "Can ggplot2 handle data of class uneval?." PSYCHOLOGICAL SCALES, 28 Jun. 2024, https://scales.arabpsychology.com/stats/can-ggplot2-handle-data-of-class-uneval/.
stats writer. "Can ggplot2 handle data of class uneval?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/can-ggplot2-handle-data-of-class-uneval/.
stats writer (2024) 'Can ggplot2 handle data of class uneval?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/can-ggplot2-handle-data-of-class-uneval/.
[1] stats writer, "Can ggplot2 handle data of class uneval?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. Can ggplot2 handle data of class uneval?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
