The “error: cannot add ggproto objects together. did you forget to add this object to a ggplot object?” error message results from a simple mistake. The good news is that it is also extremely easy to fix. If you encounter this problem, it is nothing to worry about, but it might be a little embarrassing.
Description of the error
This error message results from a remarkably simple mistake when using the ggplot and geom_line functions from the ggplot2 package. It occurs as a result of simply forgetting to include the plus sign after the ggplot function. The result is that you are not adding the two functions, but the program is still trying to perform the geom_line function without the ggplot function. Because of this fact the program encounters this error and triggers the error message. The good news is that despite being an easy mistake to make it is also an easy one to fix because all you have to do is add in the plus sign.
Explanation of the error
Here is a code example that produces this error message. It is most likely to occur if you are new to using the ggplot2 package and accidentally treat these functions like the plot function and its related functions.
> library(ggplot2)
> t = as.numeric(Sys.time())
> set.seed(t)
> A = rnorm(10)
> B = rnorm(10)
> C = rnorm(10)
> df = data.frame(A, B, C)
> df
A B C
1 -0.1381789 1.1290265718 -1.4888159
2 -0.9258970 0.4492741772 0.3731661
3 -0.2531491 0.0006993185 -1.6036893
4 0.6411098 1.2908543562 -0.6424906
5 1.0763008 0.6034466825 0.7327816
6 0.1244683 -1.6401951611 2.7876806
7 -0.9082297 0.5154387052 -0.2608329
8 1.3471557 0.1727134963 0.1209460
9 -1.8477632 1.6203027648 0.8289264
10 -0.7165551 -0.4921185654 0.7564424
> ggplot(df, aes(x = A))
> geom_line(aes(y = B, color = “B”)) +
+ geom_line(aes(y = C, color = “C”))
Error: Cannot add ggproto objects together. Did you forget to add this object to a ggplot object?
As you can see, there is no plus sign following the ggplot function. The lack of this plus sign is the cause of our error message.
How to fix the error
Here we have a code example that shows how to fix this error message. The irony is that it is extremely simple to fix because it only requires adding a plus sign after the ggplot function. It is a simple correction to make so that the two functions are added together, fixing the error, and you will no longer get our error message.
> library(ggplot2)
> t = as.numeric(Sys.time())
> set.seed(t)
> A = rnorm(10)
> B = rnorm(10)
> C = rnorm(10)
> df = data.frame(A, B, C)
> df
A B C
1 0.25453634 0.09803727 -0.73440014
2 -0.59486083 0.40165799 1.21928658
3 -0.07862049 -0.47487020 -0.39774865
4 0.93562719 0.53568494 0.63934846
5 0.64940040 0.52597289 0.35604076
6 0.65246604 1.08226861 1.59621657
7 -0.13587290 0.65033764 0.92430238
8 0.71380380 0.63878604 -0.43274753
9 2.12778067 -0.72146032 -0.84337492
10 -1.17509201 1.38660073 -0.02858734
> ggplot(df, aes(x = A)) +
+ geom_line(aes(y = B, color = “B”)) +
+ geom_line(aes(y = C, color = “C”))
If you look at this code, you will see that the only difference between this version in the earlier version is the inclusion of a plus sign after the ggplot function. Now that you are properly adding all three functions together, you will no longer get an error message but it will produce a graph of the data. It is amazing to see just how such a little mistake makes such a significant difference. The inclusion of this plus sign is all that is necessary to fix this error and produce the graph you are trying to make.
When using the ggplot function it is important to remember that you often need to add related graphing functions to it to produce the results that you are looking for. Furthermore, it is important to remember that you need to use a plus sign in doing so. Now that you understand this you can avoid the embarrassment of making this simple little mistake and getting this error message. Fortunately, it is an easy problem to fix and a problem that once you are aware of it, you will probably not make again.