How to solve the “Error: ggplot2 doesn’t know how to deal with data of class uneval”?

This article highlights the ten main causes of the error: ggplot2 doesn’t know how to deal with data of class uneval” error and how to solve it in each case.

error: ggplot2 doesn't know how to deal with data of class uneval

Failing to use the data= argument

Whenever you want to add a new data set to your geom, you must use the data= argument. In other words, you must put your arguments in the right order mapping=…, data=…for example, below are the arguments for a ?geom_line.

p + geom_line(data=df.last, aes(HrEnd, MWh, group=factor(Date)), color=”red”)
p + geom_line(aes(HrEnd, MWh, group=factor(Date)), df.last, color=”red”)

Putting data= inside aes(…)

Another cause for the “Error: ggplot2 doesn’t know how to deal with data of class uneval” error is putting the data=… accidentally inside the aes(…) instead of putting it outside. For example, this is the wrong way to put data=.

ggplot(aes(data=df[df$var7==’9-06′,],x=lifetime,y=rep_rate,group=mdcp,color=mdcp) …)

The right way would be as follows.

ggplot(data=df[df$var7==’9-06′,], aes(x=lifetime,y=rep_rate,group=mdcp,color=mdcp) …)

Prototyping the plot command with ggplot

This error may sometimes happen when you prototype the plot command with qgplot(). As you may already know, qplot(), does not use an explicit aes(). The best way to go about it would be to edit or copy, then paste it into a ggplot(). Below is an example to highlight it.

qplot(data=…, x=…,y=…, …)
ggplot(data=…, aes(x=…,y=…,…))

Missing data argument

Many experts opine that, ideally, going by the cause of “Error: ggplot2 doesn’t know how to deal with data of class uneval”, the correct error message should be something to do with “missing a data argument.

Referring to a variable data in the data frame that does not exist

Another cause of the “Error: ggplot2 doesn’t know how to deal with data of class uneval” error could be referring to a variable data in the data frame that does not exist. For example, you could forget to tell ddply to summarize one of the variables that you used in geom_line to specify your line color. As a result, ggplot doesn’t know where it can find the variable as you haven’t created it in the summary table. The error message will certainly follow.

Forgetting to pipe your ggplot with a +.

The error message could also result from your forgetting to pipe your ggplot with a +. For example, you could accidentally use the dply’s %>% operator and ggplot doesn’t get the lines it needs to complete your plot. When you fail to set up a ggplot function in a while, you’ll always tend to forget that ggplot does not use the %>% pipe in chaining its functions. You’ll always end up searching only to realize that you’ve made the same mistake again. 

Failing to specify the argument data in stat_density

The problem could also arise if you don’t specify the argument data in stat_density. Looking at ?stat_density, you’ll notice that the first argument implied is mapping=. You should, therefore, change the pdf.plot to something like this.

pdf.plot<-function(data,x,xl,yl,title,save){ ggplot() + stat_density(data = data, kernel = “biweight”,aes_string(x=x, colour=’id’)
position=”identity”,geom=”line”)+ coord_cartesian(xlim = c(0, 200))+
xlab(xl)+
ylab(yl)+
ggtitle(title)+
ggsave(save) }

Note that if you have ggplot() as your first argument in your aesthetics, then you should write your argument as follows:

ggplot(mapping = aes(…your aesthetics…) )

Failing to define your reactive function

All you need to do is define your reactive function as follows:

data <- reactive({ data.table(months = seq(1, input$months, by = 1),
value = seq(input$startingValue,
input$startingValue + input$valueChange,
length.out = input$months))
})

It’s worth noting that you do not need to define your reactive function because you have just one caller. You can put all your code in the plot section as shown below.

output$yield <- renderPlot({
data <- data.table(months = seq(1, input$months, by = 1),
value = seq(input$startingValue,
input$startingValue + input$valueChange,
length.out = input$months))
p <- ggplot(data, aes(x=months, y=value, colour=value)) +geom_line()
print(p)
})

Trying to plot latitudes and longitudes on a map 

You could also experience the “Error: ggplot2 doesn’t know how to deal with data of class uneval” error while trying to plot latitudes and longitudes on a map. The problem could be that you have extracted the coordinates from a different data set using the c(lat, lon) code, thereby creating a list when you should have instead used the cbind(lat,lon) code to create some sort of a matrix that you can then coerce into a data frame through the use of as.data.frame.

As far as arguments are concerned, if you’re using multiple datasets, try to pull the data and the aes info out of your ggplot function and intead put it in each of the geom_* objects as required. 

You’ll notice that although the first argument to your function ggplot() is data, this isn’t the case for most of the Geom_*s. The mapping is their first argument. Hence, if you’re using a dataset as the first argument, ensure you name it explicitly. That is, geom_point(data=myDataFrame,.) 

Trying to map a numeric vector to your data in geom_errorbar: GVW[1:6,3]

The “Error: ggplot2 doesn’t know how to deal with data of class uneval” error could also happen because you’re trying to map a numeric vector to your data in geom_errorbar: GVW[1:6,3]. It’s worth noting that ggplot can only work with data.frame.

Bottom Line

Generally, you should not subset inside ggplot calls because your standard error resides in four separate objects. You can add them to the original data.frame, and you’ll be able to plot everything quickly. 

Also Read: What does “Error 503 Backend fetch failed” mean?

Andrew

Andrew is a professional writer with 7+ Years of experience. His style and uniqueness inspire and educate readers throughout the world.

Recommended For You

About the Author: Andrew

Andrew is a professional writer with 7+ Years of experience. His style and uniqueness inspire and educate readers throughout the world.

Leave a Reply

Your email address will not be published. Required fields are marked *