Can somebody explain why this works:
d <- data.frame(x = 1:10, y = as.numeric(c(1:4,rep(NA,6))),z=rep(1:5,2))
ggplot(data = d, aes(x, y)) + geom_point() +facet_grid(z~.)
and when adding scales="free"
to facet_grid
an error is thrown:
d <- data.frame(x = 1:10, y = as.numeric(c(1:4,rep(NA,6))),z=rep(1:5,2))
ggplot(data = d, aes(x, y)) + geom_point() +facet_grid(z~.,scales="free")
# Error in seq.default(from = best$lmin, to = best$lmax, by = best$lstep) :
# 'from' must be of length 1
Probably it uses the min and max of all facets when scales
is not free. When scales
is free it doesn't know which value to take for the facet that contains only missings?
Is there a work-around?
I looked at two solutions.
1)
ggplot(data = d, aes(x, y)) +
geom_point() +
facet_grid(z~.,scales="free_x")
Does work, but gives the same result as without the scales="free"
part.
2)
library(gridExtra)
p1 <- ggplot(data = d[d$z==1,], aes(x, y)) + geom_point()
p2 <- ggplot(data = d[d$z==2,], aes(x, y)) + geom_point()
p3 <- ggplot(data = d[d$z==3,], aes(x, y)) + geom_point()
p4 <- ggplot(data = d[d$z==4,], aes(x, y)) + geom_point()
p5 <- ggplot(data = d[d$z==5,], aes(x, y)) + geom_point()
grid.arrange(p1,p2,p3,p4,p5,ncol=1)
This doesn't work. When plotting the plots seperately, you'll discover that p5
can't be plotted. That is due to the fact that for z=5
y
only has NA's.
Trying to use a free scale when there are only NA's isn't very logical. It is a conceptual problem in my opinion. The reason for this is that without using the scales="free"
argument, the scales of the other subplot(s) are used. When using the scales="free"
argument (or free_x
or free_y
), the scales of each subplot will be set according to the length of the scale. When there are only NA's, the length of the scale cannot be determined which in turn causes the error message.
That's the reason why free_x
does work (although it gives the same result).
To conclude: When one of your groups has only NA's, you can't use scales="free"
in your plot. Consequently, you have two options (in my opinion):
scales="free"
argument to get your desired empty subplots.0
, but this is only a solution when you don't have negative values.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With