Fixing the R error message: don’t know how to automatically pick scale for object of type data.frame. defaulting to continuous.

When doing data science in the r programming language, this error message will occur when you plot a data frame using the ggplot package. It occurs when the data frame has a column name that is the same as a function name, but it is distinguished from the function with a starting capital letter, and you use a lowercase letter instead. Fixing it simply requires, replacing the lowercase character with an uppercase one. This is a problem that could show up when you are using the r markdown package along with ggplot.

Description of the error

This is not an error message that you will encounter using base r functions, it occurs when using the ggplot function from the ggplot package. It does not result from a statistic problem resulting from the data type, value, or even missing values. It occurs because in plotting multiple columns from a data frame, you will have a problem if the column label starts with a capital letter, and you use a lowercase letter. The problem is compounded if the column name is the same as a function name and the function name starts with a lowercase letter. In this case, when you make this mistake, the system thinks you are calling the function and it produces our error message.

Explanation of the error

Here are two code examples that show the problem leading to this error message.

> library(“ggplot2”)
> df = data.frame(Score = 10:15,
+ Team = letters[1:6])
> df
Score Team
1 10 a
2 11 b
3 12 c
4 13 d
5 14 e
6 15 f
> ggplot(df, aes(Team, score)) + geom_bar(stat = “identity”)
Error in FUN(X[[i]], …) : object ‘score’ not found

In this example, the continuous scale variable forms the vertical axis, but in the ggplot “score” starts with a lowercase letter when the column lable “Score” starts with an uppercase letter. This does not produce our error message, but it does produce another error message.

> library(“ggplot2”)
> df = data.frame(Mean = 10:15,
+ Team = letters[1:6])
> df
Mean Team
1 10 a
2 11 b
3 12 c
4 13 d
5 14 e
6 15 f
> ggplot(df, aes(Team, mean)) + geom_bar(stat = “identity”)
Don’t know how to automatically pick scale for object of type function. Defaulting to continuous.
Error: Aesthetics must be valid data columns. Problematic aesthetic(s): y = mean.
Did you mistype the name of a data column or forget to add after_stat()?
Run `rlang::last_error()` to see where the error occurred.

In this example, the continuous scale variable forms the vertical axis, but in ggplot “mean” starts with a lowercase letter when the column lable “Mean” starts with an uppercase letter. This produces our error message, because “mean” is also the name of a function. The result is that the system gets confused.

How to fix the R error

Here we have two examples for fixing this problem.

> library(“ggplot2”)
> df = data.frame(Score = 10:15,
+ Team = letters[1:6])
> df
Score Team
1 10 a
2 11 b
3 12 c
4 13 d
5 14 e
6 15 f
> ggplot(df, aes(Team, Score)) + geom_bar(stat = “identity”)

In this example, we fix the problem by using “Score” in ggplot rather than ”score.” The result is a bar chart with a character vector as its horizontal axis.

> library(“ggplot2”)
> df = data.frame(Mean = 10:15,
+ Team = letters[1:6])
> df
Mean Team
1 10 a
2 11 b
3 12 c
4 13 d
5 14 e
6 15 f
> ggplot(df, aes(Team, Mean)) + geom_bar(stat = “identity”)

In this example, we fix the error message by using “Mean” in ggplot rather than “mean.” The result is a bar chart with a character vector as its horizontal axis.

This is an unusual error message because it only shows up when you make a mistake on a word that is the same as a function’s name. Otherwise, you get a completely different error message by making the same mistake. This detail makes this particular error message hard to understand, but once you understand what is going on it is easy to fix. You actually learn to fix two error messages at the same time.

Scroll to top
Privacy Policy