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

 

reference weblink
stackoverflow
https://stackoverflow.com/questions/20155581/persistent-invalid-graphics-state-error-when-using-ggplot2

solution:
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.
运行dev.off()语句,重新运行绘图语句就好了,有点类似于windows的注销操作或清理缓存。