Rolf Turner
2019-Dec-01 11:46 UTC
[R] Still struggling with facet_grid_paginate() from package ggforce.
I am trying to produce a ggplot2 graphic in which there is a single conditioning variable with a large number of levels (24). If I use facet_grid() I get a plot with either 24 rows or 24 columns, both of which look like hell. I thought that facet_grid_paginate() would rescue me, but it doesn't seem to. I ask for 3 rows and 4 columns, and thought that I would get two 3 x 4 pages Instead I get six pages with only one row (of four facets) per page. Am I misunderstanding something? Doing something silly? Or is this a bug? I have attached a reproducible example, along with the data set on which it depends. Grateful for any insight. cheers, Rolf Turner -- Honorary Research Fellow Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276 -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: reprex.txt URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20191202/bd4728db/attachment.txt> -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: egDat.txt URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20191202/bd4728db/attachment-0001.txt>
Rui Barradas
2019-Dec-01 21:45 UTC
[R] Still struggling with facet_grid_paginate() from package ggforce.
Hello,
Here are two ways.
The first is an adaptation from your code. It uses facet_wrap_paginate,
not *_grid_*.
plotObj2 <- vector("list",2)
for(pg in 1:2) {
plotObj2[[pg]] <- ggplot(egDat) +
geom_point(aes(y = obsd, x = x),
na.rm = TRUE, shape = 20, colour = "blue") +
geom_line(aes(y = fit2, x = cPred)) +
facet_wrap_paginate(facets = ~Trt,
ncol = 4, nrow = 3, page = pg) +
theme_bw()
}
print(plotObj2)
The second is an adaptation of SO[1]. It needs two calls to the plot
code and it's slower but gets the job done.
g <- ggplot(egDat) +
geom_point(aes(y = obsd, x = x),
na.rm = TRUE, shape = 20, colour = "blue") +
geom_line(aes(y = fit2, x = cPred)) +
facet_wrap_paginate(facets = ~Trt, ncol = 4, nrow = 3, page = 1) +
theme_bw()
n <- n_pages(g)
for(i in 1:n){
print(g + facet_wrap_paginate(~Trt, ncol = 4, nrow = 3, page = i))
}
print(g)
Hope this helps,
Rui Barradas
[1] https://stackoverflow.com/a/58373858/8245406
?s 11:46 de 01/12/19, Rolf Turner escreveu:>
> I am trying to produce a ggplot2 graphic in which there is a single
> conditioning variable with a large number of levels (24).
>
> If I use facet_grid() I get a plot with either 24 rows or 24 columns,
> both of which look like hell.
>
> I thought that facet_grid_paginate() would rescue me, but it doesn't
> seem to.? I ask for 3 rows and 4 columns, and thought that I would get
> two 3 x 4 pages? Instead I get six pages with only one row (of four
> facets) per page.
>
> Am I misunderstanding something?? Doing something silly?? Or is this a bug?
>
> I have attached a reproducible example, along with the data set on which
> it depends.
>
> Grateful for any insight.
>
> cheers,
>
> Rolf Turner
>
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
Rolf Turner
2019-Dec-02 00:45 UTC
[R] Still struggling with facet_grid_paginate() from package ggforce.
On 2/12/19 10:45 am, Rui Barradas wrote:> Hello, > > Here are two ways. > > The first is an adaptation from your code. It uses facet_wrap_paginate, > not *_grid_*. > > > plotObj2 <- vector("list",2) > for(pg in 1:2) { > ? plotObj2[[pg]] <- ggplot(egDat) + > ??? geom_point(aes(y = obsd, x = x), > ?????????????? na.rm = TRUE, shape = 20, colour = "blue") + > ??? geom_line(aes(y = fit2, x = cPred)) + > ??? facet_wrap_paginate(facets = ~Trt, > ??????????????????????? ncol = 4, nrow = 3, page = pg) + > ??? theme_bw() > } > print(plotObj2) > > > The second is an adaptation of SO[1]. It needs two calls to the plot > code and it's slower but gets the job done. > > > g <- ggplot(egDat) + > ? geom_point(aes(y = obsd, x = x), > ???????????? na.rm = TRUE, shape = 20, colour = "blue") + > ? geom_line(aes(y = fit2, x = cPred)) + > ? facet_wrap_paginate(facets = ~Trt, ncol = 4, nrow = 3, page = 1) + > ? theme_bw() > > n <- n_pages(g) > for(i in 1:n){ > ? print(g + facet_wrap_paginate(~Trt, ncol = 4, nrow = 3, page = i)) > } > > print(g) > > > > Hope this helps.Indeed it did. You are brilliant, Rui. Problem solved. I note that one can, I think, calculate the number of pages a priori, thus avoiding two calls to ggplot(), in something like the following manner: nface <- with(Dat,prod(sapply(condVars, function(x){length(levels(get(x)))}))) npgs <- ceiling(nface/(nrow*ncol)) where Dat is the data frame of data being plotted and condVars is a vector of the names of the variables being conditioned on (i.e. the variables determining the facets). In the example that I provided, condVars would just be "Trt", but it all seems to work in settings in which there is more than one conditioning variable. Thanks very much. cheers, Rolf -- Honorary Research Fellow Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276