Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird ggplot2 error: Empty raster

Tags:

r

ggplot2

Why does

ggplot(data.frame(x=c(1,2),y=c(1,2),z=c(1.5,1.5)),aes(x=x,y=y,color=z)) +
geom_point()

give me the error

Error in grid.Call.graphics(L_raster, x$raster, x$x, x$y, x$width, x$height, : Empty raster

but the following two plots work

ggplot(data.frame(x=c(1,2),y=c(1,2),z=c(2.5,2.5)),aes(x=x,y=y,color=z)) +
geom_point()
ggplot(data.frame(x=c(1,2),y=c(1,2),z=c(1.5,2.5)),aes(x=x,y=y,color=z)) +
geom_point()

I'm using ggplot2 0.9.3.1

like image 823
Ben Avatar asked Apr 11 '14 04:04

Ben


2 Answers

TL;DR: Check your data -- do you really want to use a continuous color scale with only one possible value for the color?

The error does not occur if you add + scale_fill_continuous(guide=FALSE) to the plot. (This turns off the legend.)

ggplot(data.frame(x=c(1,2), y=c(1,2), z=c(1.5,1.5)), aes(x=x,y=y,color=z)) +
  geom_point() + scale_color_continuous(guide = FALSE)

The error seems to be triggered in cases where a continuous color scale uses only one color. The current GitHub version already includes the relevant pull request. Install it via:

devtools::install_github("hadley/ggplot2")

But more probably there is an issue with the data: why would you use a continuous color scale with only one value?

like image 191
krlmlr Avatar answered Oct 23 '22 14:10

krlmlr


The same behaviour (i.e. the "Empty raster"error) appeared to me with another value apart from 1.5.

Try the following:

ggplot(data.frame(x=c(1,2),y=c(1,2),z=c(0.02,0.02)),aes(x=x,y=y,color=z))
+ geom_point()

And you get again the same error (tried with both 0.9.3.1 and 1.0.0.0 versions) so it looks like a nasty and weird bug.

like image 42
Costas B. Avatar answered Oct 23 '22 14:10

Costas B.