158

I believe my dataframe is okay and my code is okay. In fact, I have eliminated parts of the dataframe and most of the graphing code to make things as basic as possible. But still, I get:

Error in .Call.graphics(C_palette2, .Call(C_palette2, NULL)) : 
  invalid graphics state

What is wrong here? Here is the data:

 date   trt var val
1/8/2008    cc  sw5 0.2684138
1/8/2008    cc  sw15    0.2897586
1/8/2008    cc  sw5 0.2822414
2/8/2008    cc  sw5 0.2494583
2/8/2008    cc  sw5 0.2692917
2/8/2008    cc  sw15    0.2619167
2/8/2008    cc  sw5 0.204375
3/8/2008    cc  sw5 0.2430625
3/8/2008    cc  sw5 0.2654375
3/8/2008    cc  sw5 0.2509583
3/8/2008    cc  sw5 0.2055625
1/8/2008    ccw sw15    0.2212414
1/8/2008    ccw sw5 0.3613448
1/8/2008    ccw sw5 0.2607586
2/8/2008    ccw sw5 0.2087917
2/8/2008    ccw sw15    0.3390417
2/8/2008    ccw sw5 0.2436458
2/8/2008    ccw sw5 0.290875
3/8/2008    ccw sw5 0.20175
3/8/2008    ccw sw15    0.328875
3/8/2008    ccw sw5 0.2328958
3/8/2008    ccw sw5 0.2868958

When I work with this data, I specify dates like this:

df<-df[order(as.Date(df$date,format="%d/%m/%Y")),,drop=FALSE]

and here I want to make a scatterplot:

ggplot(data = df,aes(x = date,y = val)) + 
 geom_point(aes(group = trt))
5
  • 4
    Have you tried (re)installing ggplot2? Commented Nov 22, 2013 at 22:25
  • I added this as an answer, in that way other people can find the answer more easily. You can accept the answer by clicking on the tick mark right next to my answer. Just to confirm, you had ggplot2 installed originally? Commented Nov 23, 2013 at 6:51
  • Yes, it was already installed. Commented Dec 13, 2013 at 20:43
  • 7
    restarting R session solved my problem. Commented Sep 5, 2014 at 9:29
  • FYI - this issue can also arise if you attempt to construct use figure dimensions that are too small (e.g. attempting to provide dimensions in inches without specifying the unit). Commented Apr 25, 2018 at 18:50

8 Answers 8

456

I ran into this same error and solved it by running:

dev.off()

and then running the plot again. I think the graphics device was messed up earlier somehow by exporting some graphics and it didn't get reset. This worked for me and it's simpler than reinstalling ggplot2.

Sign up to request clarification or add additional context in comments.

5 Comments

This solves the problem, but do you have any idea as to why it does?
@PedroCavalcanteOliveira I had the problem, after I used par(mfrow = c(..., ...)) before. Don't know about other graphic parameters but they might cause this problem.
It helped but what is does?
Running graphics.off()also worked for me. According to the R documentation, "graphics.off() shuts down all open graphics devices."
I can't explain why this worked for me, too, when using ggcorrplot but it did work. I knew there was something machine specific because it worked on one laptop but not the other with exact same R Versions and R Code.
13

I found this to occur when you mix ggplot charts with plot charts in the same session. Using the 'dev.off' solution suggested by Paul solves the issue.

2 Comments

What dev.off' does?
It shuts down the current graphics device.
10

The solution is to simply reinstall ggplot2. Maybe there is an incompatibility between the R version you are using, and your installed version of ggplot2. Alternatively, something might have gone wrong while installing ggplot2 earlier, causing the issue you see.

2 Comments

Thanks for keeping this answer here. Although the other answer is simpler, dev.off() didn't work for me and it is a good second option to try reinstalling ggplot2.
It may be better to try dev.off() first, takes less time than reinstall package. This can be a second step if dev.off() didn't work.
4

You likely don't need to reinstall ggplot2

Solution: go back to plot that didn't work previously. Take the below console output for example. The figure margins (the window that displays your plots) were too small to display the pairs(MinusInner) plot. Then when I tried to make the next qplot, R was still hung up on previous error.

pairs(MinusInner) Error in plot.new() : figure margins too large qplot(Sample.Type, BAE,data=MinusInner, geom="boxplot") Error in .Call.graphics(C_palette2, .Call(C_palette2, NULL)) : invalid graphics state

I fixed the first error by expanding the plot window and rerunning the pairs(MinusInner) plot. Then blam, it worked.

pairs(MinusInner) qplot(Sample.Type, BAE,data=MinusInner, geom="boxplot")

Comments

3

Restarting R (Menu>Sessions>Restart R or ⌘⇧F10) and re-running my code fixed the problem.

Comments

1

I solved this by clearing all the plots in the console and then making sure the plot area was large enough to accommodate what I was creating.

Comments

1

To add, an easy fix if you're using RStudio will be expanding the size of your plot windows/area by dragging it apart. Basically, this problem occurred because your current plot window is smaller than required to display the plot.

Comments

0

try to get out grafics with x11() or win.graph() and solve this trouble.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.