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. you can also use the na.omit(dataframe). That worked for me. I had only one (!) NA in 722K rows of data. It was enough to get this error.
We could just solve this problem and the source was we were using the Capital X instead of the small x in the formula, the wrong one is: ggplot(isotidy, aes(X=site, y= dN_fish, fill =site)) + geom_boxplot() and is supposed to be like below: ggplot(isotidy, aes(x =site, y= dN_fish, fill =site)) + geom_boxplot()
I hope this help
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.