4

I was running the xyplot of presidential elections in the pscl package and got the error

data(presidentialElections)
library(lattice)
xyplot(demVote ~ year | state,
       panel=panel.lines,
       ylab="Democratic Vote for President (percent)",
       xlab="Year",
       data=presidentialElections,
       scales=list(y=list(cex=.6),x=list(cex=.35)),
       strip=strip.custom(par.strip.text=list(cex=.6)))

Error:
Error in recordGraphics(drawGrob(x), list(x = x), getNamespace("grid")) :
  invalid graphics state

I am a new user to R and would appreciate if anyone can help me to fix the error.

www
  • 38,575
  • 12
  • 48
  • 84
Metrics
  • 15,172
  • 7
  • 54
  • 83
  • 4
    works fine for me. I hate to suggest something boneheaded, but have you tried restarting your R session? (Are you by any chance running this in Rstudio?) You may possibly be running into trouble because the graph is very big and R may be having trouble allocating space for margins etc. – Ben Bolker Jul 02 '12 at 14:11
  • works fine for me too, also in RStudio. What version of lattice and R are you using? – EDi Jul 02 '12 at 14:18
  • Thanks a lot. It worked when I restarted. Yes, I am using R studio. Is this type of problem common in R studio? – Metrics Jul 02 '12 at 14:19
  • 3
    I think I have seen it before, but only very occasionally and not reproducibly. If you encounter this kind of problem frequently enough that it is a nuisance, it would be good if you could try to (1) isolate a reproducible example (2) edit your question to post it here and allow people to test it on different platforms (3) if it turns out to be rstudio-specific, post it on the Rstudio forum (the maintainers are very responsive). I suspect it will be hard to reproduce though ... https://groups.google.com/forum/?fromgroups#!topic/ggplot2/XwrTspw8LRw gives a similar answer. – Ben Bolker Jul 02 '12 at 16:15
  • Did you run this code in RStudio? In my case, I can run code in pure R but not in RStudio. After I input dev.off(), it worked. But I still don't understand why. – Jeffrey May 09 '14 at 01:44

2 Answers2

3

Try dev.off() and then re-run the same code.

Anton Tarasenko
  • 8,099
  • 11
  • 66
  • 91
0

I personally use tinn r to program in R and find it very useful, you can close/open R easily and still send command lines in. (you can even DL different versions of R to load for different packages)

Riley
  • 23
  • 1
  • 1
  • 4