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

create.colourkey opens new device and draws even when assigned to variable #140

Open
stefaneng opened this issue Jul 20, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@stefaneng
Copy link
Contributor

create.colourkey is a wrapper around lattice::draw.colorkey. We have set the parameter draw = TRUE which will print the key on whatever device/page is open or open a new device. I think this is a strange forced argument and should probably be an argument to create.colourkey with the default as draw = FALSE.

Code in question: https://github.com/uclahs-cds/public-R-BoutrosLab-plotting-general/blob/main/R/create.colourkey.R#L168

See the workaround in the discussion: #139

Example

Look at the top of the heatmap. The colorkey is drawn on top when rendered via reprex but generally will often open a new device which generally means an extra file is created.

library(BoutrosLab.plotting.general);
#> Loading required package: lattice
#> Loading required package: latticeExtra
#> Loading required package: cluster
#> Loading required package: hexbin
#> Loading required package: grid
#> 
#> Attaching package: 'BoutrosLab.plotting.general'
#> The following object is masked from 'package:stats':
#> 
#>     dist

set.seed(13);
dat1 <- matrix(rnorm(100), nrow = 10, ncol = 10);
dat2 <- matrix(rnorm(100), nrow = 10, ncol = 10);

at <- seq(-3, 3, length.out = 10);
p1 <- create.heatmap(
    dat1,
    at = at,
    clustering.method = 'none',
    same.as.matrix = TRUE,
    print.colour.key = FALSE
    );

p2 <- create.heatmap(
    dat2,
    at = at,
    clustering.method = 'none',
    same.as.matrix = TRUE,
    print.colour.key = FALSE
    );

# NOTE: This is a bug in create.colourkey. Opens a null device to write the output to
# png(nullfile())
colourkey <- create.colourkey(
    x = dat1,
    at = at,
    colourkey.labels.cex = 2.5,
    placement = viewport(
        just='center',
        x = 0.5,
        y = 1,
        width = 0.5,
        height = 0.1
        )
    );

# NOTE: This is a bug in create.colourkey. Close the null device we opened
# dev.off();

filename <- tempfile('example_shared_colourkey', fileext = '.png');
create.multipanelplot(
    plot.objects = list(p1, p2),
    layout.width = 2,
    layout.height = 1,
    width = 10,
    height = 6,
    resolution = 200,
    legend = list(
        bottom = list(
            fun = colourkey
            )
        ),
    filename = filename
    );

# To ensure that width/height are maintained by github/reprex
# All this does it load and print the png we saved
grid::grid.raster(png::readPNG(filename))

Created on 2023-07-20 with reprex v2.0.2

@stefaneng stefaneng added the bug Something isn't working label Jul 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant