Fixing the R error in .call.graphics(c_palette2, .call(c_palette2, null)) : invalid graphics state

This error message occurs when using the ggplot function after plotting another data set or having changed some of the plotting parameters using functions such as the par and mfrow functions together. To fix this error when it occurs, do a reset using the dev.off function. This is an error message that you may or may not get even if you do everything mentioned here. Fortunately, it does not occur all the time.

Description of the R Error

The ggplot function is part of the ggplot2 library and this is a periodic error message that you may get when using this function. What causes it is a change in the graphics settings that is incompatible with the graphing performed by the ggplot function. This incompatibility triggers our error message. These settings can be changed when plotting other data sets or when using the par and mfrow functions together. The good news is that this error message does not always occur, but at the same time this can make it even more frustrating. It makes it like the periodic problem with your car that never seems to show up when your mechanic is looking at it. You may even be annoyed that the code always works on the other person’s computer. Fortunately, this error message is extremely easy to fix.

Explanation of the R Error

This is an example of the error message. Unfortunately, I Have no guaranteed way of triggering this error message. It results from a complex set of circumstances that cannot always be duplicated.

> library(“ggplot2”)
> data = data.frame(x = 5:1,
+ y = 1:5)
> data
x y
1 5 1
2 4 2
3 3 3
4 2 4
5 1 5
> ggplot(data, aes(x, y)) + geom_point()
Error in .Call.graphics(C_palette2, .Call(C_palette2, NULL)) :
invalid graphics state

This example illustrates what the results are when you get this error message. Fortunately, it is easy to fix when it does show up.

How to fix the R error

Here we have an example of how to fix this error message it is an amazingly simple process. It simply requires including an additional function to reset the graphics settings.

> library(“ggplot2”)
> data = data.frame(x = 5:1,
+ y = 1:5)
> data
x y
1 5 1
2 4 2
3 3 3
4 2 4
5 1 5
> dev.off()
null device
1
> ggplot(data, aes(x, y)) + geom_point()

All that is necessary to fix this problem when it occurs is to add the dev.off function to your code. This will reset the graphics settings fixing this error.

This is an error message that fortunately is not a direct result of your code. You did not make a coding error. It is a result of factors that can change in the graphics settings so that they are no longer compatible with the ggplot function. Fortunately, this error message is easy to fix. Once you know this simple fix, you will know how to solve this problem when it comes up.

Scroll to top
Privacy Policy