Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Desc does not always reset mfrow after creating plots #49

Open
ajcullum opened this issue Jun 27, 2020 · 5 comments
Open

Desc does not always reset mfrow after creating plots #49

ajcullum opened this issue Jun 27, 2020 · 5 comments

Comments

@ajcullum
Copy link

When Desc creates output that show multiple graphs per plot(? Unsure of the terminology), it appears to do this by changing par(mfrow()) and layout(). But those settings are not always returned to the default c(1,1) afterward. The result is that later graphs do not fill the plot area.

The demo code below shows the error. I've seen it occur in other uses of Desc() as well, but this happened before I figured out the issue, and so I didn't make note of the particular arguments that triggered it.

# With default par() values active
db <- structure(list(Group = structure(sample.int(2, 10, replace=T), 
      .Label = c("A", "B"), class = "factor"), 
      Value = sample.int(100, 10, replace=T)), 
      row.names = c(NA, 10L), class = "data.frame")

# This use of Desc() does *not* cause the error
Desc(db$Value~db$Group)  # Gives side-by-side graphs
# Check the layout afterwards
layout.show(2) #  Future graphs will fill the plot area

# This (somewhat unusual) call *does* trigger the error
Desc(db$Group~db$Value)  # Gives side-by-side graphs
# Check the layout afterwards
layout.show(2) # Future graphs will also be in pairs, side-by-side

Thanks!

@AndriSignorell
Copy link
Owner

This is indeed not a convincing design, which derives from a very early version of DescTools.
I will go over that.

@GegznaV
Copy link
Contributor

GegznaV commented Feb 28, 2023

Here is just a reprex of the code by @ajcullum with outputs and plots:

library(DescTools)

# With default par() values active
db <- structure(list(Group = structure(sample.int(2, 10, replace=T), 
      .Label = c("A", "B"), class = "factor"), 
      Value = sample.int(100, 10, replace=T)), 
      row.names = c(NA, 10L), class = "data.frame")

# This use of Desc() does *not* cause the error
Desc(db$Value~db$Group)  # Gives side-by-side graphs
#> ------------------------------------------------------------------------------ 
#> db$Value ~ db$Group
#> 
#> Summary: 
#> n pairs: 10, valid: 10 (100.0%), missings: 0 (0.0%), groups: 2
#> 
#>                         
#>               A        B
#> mean     44.600   16.000
#> median   59.000   13.000
#> sd       27.682   17.059
#> IQR      37.000   11.000
#> n             5        5
#> np      50.000%  50.000%
#> NAs           0        0
#> 0s            0        0
#> 
#> Kruskal-Wallis rank sum test:
#>   Kruskal-Wallis chi-squared = 3.1527, df = 1, p-value = 0.0758

# Check the layout afterwards
layout.show(2) #  Future graphs will fill the plot area

# This (somewhat unusual) call *does* trigger the error
Desc(db$Group~db$Value)  # Gives side-by-side graphs
#> ------------------------------------------------------------------------------ 
#> db$Group ~ db$Value
#> 
#> Summary: 
#> n pairs: 10, valid: 10 (100.0%), missings: 0 (0.0%), groups: 2
#> 
#>                         
#>               A        B
#> mean     44.600   16.000
#> median   59.000   13.000
#> sd       27.682   17.059
#> IQR      37.000   11.000
#> n             5        5
#> np      50.000%  50.000%
#> NAs           0        0
#> 0s            0        0
#> 
#> Kruskal-Wallis rank sum test:
#>   Kruskal-Wallis chi-squared = 3.1527, df = 1, p-value = 0.0758
#> 
#> 
#> 
#> 
#> 
#> Proportions of db$Group in the quantiles of db$Value:
#>    
#>           Q1       Q2       Q3       Q4
#>   A    33.3%     0.0%    50.0%   100.0%
#>   B    66.7%   100.0%    50.0%     0.0%
#> Warning in par(usr): argument 1 does not name a graphical parameter

# Check the layout afterwards
layout.show(2) # Future graphs will also be in pairs, side-by-side

Created on 2023-02-28 with reprex v2.0.2

@GegznaV
Copy link
Contributor

GegznaV commented Feb 9, 2024

Is it possible to solve this by using something like on.exit() in an appropriate place?

@GegznaV
Copy link
Contributor

GegznaV commented Feb 25, 2024

I think the original issue is solved, but some lines of code should be rewritten to make code more robust, eg in these and some other lines of code may need more attention:

DescTools/R/Desc.R

Lines 2832 to 2833 in 388487e

# # reset layout
# layout(1)

DescTools/R/Desc.R

Lines 2936 to 2937 in 388487e

# reset layout
layout(1)

layout(matrix(1)) # reset layout on exit

I recommend resetting via on.exit().

@AndriSignorell
Copy link
Owner

This should also be fixed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants