0

In my package, I like to create a mosaic plot. In the interactive session, this works well. However, if I call the function from an fresh R session with devtools::load_all, the error "Discrete value supplied to continuous scale" is raised.

Here is a minimum working example:

test_ggmosaic <- function(dat) {
    dat <- datasets::mtcars
    p <- ggplot2::ggplot(data=dat)+
        ggmosaic::geom_mosaic(
            ggplot2::aes(weight=mpg,x=ggmosaic::product(gear, cyl), fill=mpg)
        ) +
        ggplot2::theme_bw()+
        ggplot2::scale_fill_discrete(guide=ggplot2::guide_legend(reverse=TRUE)
    )
    return(p)
}

Now when this function is defined inside a package, and ggplot2 and ggmosaic are added to the Imports field in the DESCRIPTION file, and I call this function after loading devtools and (via load_all) the package the function is in, I get the error

Fehler: Discrete value supplied to continuous scale

When, on the other hand, I first execute

library(ggplot2)
library(ggmosaic)

and then call the function above, the plot appears (and does not make much sense in this example).

Karsten W.
  • 17,826
  • 11
  • 69
  • 103
  • 1
    Hej Karsten. Hm. The general issue which gives rise to this error is when you are mapping a categorical variable on a continuous scale, e.g. `ggplot(mtcars, aes(hp, mpg, fill = factor(cyl))) + scale_fill_continuous()`. – stefan Dec 22 '21 at 10:19
  • This can't be the case here, since the same code works if I call `library(ggplot2)` and `library(ggmosaic)` first... – Karsten W. Dec 22 '21 at 15:30
  • 1
    It’s possible that `data(mtcars)` inside a package doesn’t do the right thing. At any rate, inside a package you’ll want to use `datasets::mtcars` instead. – Konrad Rudolph Dec 22 '21 at 16:06
  • thanks, I fixed that. The problem seems to be elsewhere, since my original function does not make use of mtcars. – Karsten W. Dec 22 '21 at 16:09
  • 2
    Hm. I just had a look and I'm able to reproduce your issue. I'm quite sure that this is a ggmosaic issue, e.g. when you try with `geom_point` instead of `geom_mosaic` everything works fine. Moreover, I tried with using the `.data` pronoun, i.e. use `.data$mpg` and so on. In that case we don't get an error but only a warning raised by `stat_mosaic`. But still no mosaic plot shows up. – stefan Dec 22 '21 at 16:11
  • 1
    Thanks, I posted an issue on github: https://github.com/haleyjeppson/ggmosaic/issues/58 – Karsten W. Dec 22 '21 at 16:44

0 Answers0