Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

geom_blank drops NA

Tags:

r

ggplot2

Using geom_blank I want to add some new factor levels, but I can't seem to do this and keep the NA level

library('ggplot2')
pl <- ggplot(data.frame(x = factor(c(1:2, NA)), y = 1), aes(x, y)) + geom_point()
pl

enter image description here

pl + geom_blank(data = data.frame(x = addNA(factor(c(0:3, NA))), y = 1))

enter image description here

I would like to have the x at 0,1,2,3,NA using geom_blank

like image 354
rawr Avatar asked Sep 22 '16 20:09

rawr


1 Answers

As I hoped in the comments, a work-around could be re-ordering the layers if the plot is already made, and this should work for ggplots in general.

library('ggplot2')
pl <- ggplot(data.frame(x = factor(c(1:2, NA)), y = 1), aes(x, y)) +
  geom_point() +
  geom_blank(data = data.frame(x = addNA(factor(c(0:3, NA))), y = 1))


## not what I want
pl


## this is what I want
pl$layers <- rev(pl$layers)
pl

enter image description here

like image 103
rawr Avatar answered Oct 25 '22 01:10

rawr