[R] Can't get facet_grid_paginate() from ggforce to work.

Rolf Turner r@turner @end|ng |rom @uck|@nd@@c@nz
Mon Nov 18 00:56:24 CET 2019


Yep.  That worked.  Thanks very much Rui.

cheers,

Rolf

On 18/11/19 2:12 AM, Rui Barradas wrote:

> Hello,
> 
> Sorry, copy&paste did a poor job. The right code now.
> 
> 
> library(ggforce)
> 
> X <- dget("testData.txt")
> 
> nrows <- length(levels(X$Species))
> ncols <- length(levels(X$LifeStage))
> npages <- length(levels(X$degC))
> 
> plotObj <- vector("list", npages)
> 
> pdf(file = 'Rhelp_test.pdf')
> for(page in 1:npages) {
>    plotObj[[page]] <- ggplot(X) +
>      geom_point(aes(y = Sfrac , x = x)) +
>      facet_grid_paginate(facets = Species ~ LifeStage:degC,
>                          page = page,
>                          nrow = nrows, ncol = ncols)+
>      ylab("Success fraction") +
>      theme_bw()
>    print(ggplot(X) +
>      geom_point(aes(y = Sfrac , x = x)) +
>      facet_grid_paginate(facets = Species ~ LifeStage:degC,
>                          page = page,
>                          nrow = nrows, ncol = ncols)+
>      ylab("Success fraction") +
>      theme_bw()
>    )
> }
> dev.off()
> 
> print(plotObj[[1]])
> print(plotObj[[2]])
> 
> 
> Hope this helps,
> 
> Rui Barradas
> 
> Às 13:07 de 17/11/19, Rui Barradas escreveu:
>> Hello,
>>
>> I don't know why but use an explicit nrow = nrows with 'nrows' defined as
>>
>> nrows <- length(levels(X$Species))
>>
>> It seems to work (remove the device pdf() code if it's more annoying 
>> than useful):
>>
>>
>> nrows <- length(levels(X$Species))
>> ncols <- length(levels(X$LifeStage))
>> npages <- length(levels(X$degC))
>>
>> plotObj <- vector("list", npages)
>>
>> pdf(file = 'Rhelp_test.pdf')
>> for(page in 1:npages) {nrows <- length(levels(X$Species))
>> ncols <- length(levels(X$LifeStage))
>> npages <- length(levels(X$degC))
>>
>> plotObj <- vector("list", npages)
>>
>> pdf(file = 'Rhelp_test.pdf')
>> for(page in 1:npages) {
>>    plotObj[[page]] <- ggplot(X) +
>>      geom_point(aes(y = Sfrac , x = x)) +
>>      facet_grid_paginate(facets = Species ~ LifeStage:degC,
>>                          page = page,
>>                          nrow = nrows, ncol = ncols)+
>>      ylab("Success fraction") +
>>      theme_bw()
>>    print(ggplot(X) +
>>      geom_point(aes(y = Sfrac , x = x)) +
>>      facet_grid_paginate(facets = Species ~ LifeStage:degC,
>>                          page = page,
>>                          nrow = nrows, ncol = ncols)+
>>      ylab("Success fraction") +
>>      theme_bw()
>>    )
>> }
>> dev.off()
>>
>> print(plotObj[[1]])
>> print(plotObj[[2]])
>>
>>    plotObj[[page]] <- ggplot(X) +
>>      geom_point(aes(y = Sfrac , x = x)) +
>>      facet_grid_paginate(facets = Species ~ LifeStage:degC,
>>                          page = page,
>>                          nrow = nrows, ncol = ncols)+
>>      ylab("Success fraction") +
>>      theme_bw()
>>    print(ggplot(X) +
>>      geom_point(aes(y = Sfrac , x = x)) +
>>      facet_grid_paginate(facets = Species ~ LifeStage:degC,
>>                          page = page,
>>                          nrow = nrows, ncol = ncols)+
>>      ylab("Success fraction") +
>>      theme_bw()
>>    )
>> }
>> dev.off()
>>
>> print(plotObj[[1]])
>> print(plotObj[[2]])
>>
>>
>> Hope this helps,
>>
>> Rui Barradas
>>
>> Às 23:11 de 16/11/19, Rolf Turner escreveu:
>>>
>>> Clearly there's something that I'm not understanding, but 
>>> facet_grid_paginate() seems to be ignoring the "ncol" argument.
>>>
>>> Here's a reprex:
>>>
>>> library(ggforce)
>>> X <- dget("testData.txt")
>>> ncols <- length(levels(X$LifeStage))
>>> npages <- length(levels(X$degC))
>>> plotObj <- vector("list",npages)
>>> for(page in 1:npages) {
>>> plotObj[[page]] <- ggplot(X) +
>>>      geom_point(aes(y = Sfrac , x = x)) +
>>>      facet_grid_paginate(facets=Species~LifeStage:degC,
>>>                          page=page,ncol=ncols)+
>>>      ylab("Success fraction") +
>>>      theme_bw()
>>> }
>>>
>>> The data set "testData.txt" is attached.
>>>
>>> You will see if you "print" plotObj[[1]] that the plot has 8 columns, 
>>> not 4 (4 being the value of "ncols").
>>>
>>> The first and second pages seem to be identical; 
>>> facet_grid_paginate() has put everything on one page, and then 
>>> repeated that page.
>>>
>>> I must be doing something utterly stupid, but I can't see what it is.
>>>
>>> The example given in the help for ggforce, using the diamonds data, 
>>> looks to me just like my example, except that it works and my example 
>>> doesn't! What am I missing?
>>>
>>> What I'm after is something like the plot in the attached pdf file
>>> test.pdf (which I managed to produce using lattice).
>>>
>>> Can anyone point me in the right direction?
>>>
>>> Thanks.
>>>
>>> cheers,
>>>
>>> Rolf Turner



More information about the R-help mailing list