Search code examples
rplottext

How to add legend in consort plot?


I need to add legend in a consort plot generate by the {consort} package, but I can't.

I tried inserting with the text function:

plot(fluxo)
text(1, 1, "text")

Error in text.default(0, 0, "text") : plot.new has not been called yet

So:

plot(fluxo)
plot.new()

Error in plot.new() : figure margins too large

Is there solution?


Solution

  • That call to plot() is governed by S3, R’s first and simplest object oriented system.

    I assume your fluxo object is of type "consort". That means the plot(fluxo) ends up calling consort:::plot.consort(fluxo).

    Here is the source code for consort:::plot.consort:

    function (x, grViz = FALSE, ...) 
    {
        if (!grViz) {
            r <- build_grid(x)
            grid.newpage()
            grid.draw(r)
        }
        else {
            if (requireNamespace("DiagrammeR", quietly = TRUE)) {
                grviz_txt <- build_grviz(x)
                DiagrammeR::grViz(grviz_txt)
            }
            else {
                stop("package `DiagrammeR` is needed to draw grViz plot.")
            }
        }
    }
    

    By default the function plots using the {grid} package. Therefore, use grid functions to edit the plot.

    Here is a full reprex using the example from the {consort} package:

    library(consort)
    library(grid)
    
    set.seed(1001)
    N <- 300
    
    trialno <- sample(c(1000:2000), N)
    exc <- rep(NA, N)
    exc[sample(1:N, 15)] <- sample(c("Sample not collected", "MRI not collected", "Other"),
                                   15, replace = T, prob = c(0.4, 0.4, 0.2))
    
    arm <- rep(NA, N)
    arm[is.na(exc)] <- sample(c("Conc", "Seq"), sum(is.na(exc)), replace = T)
    
    fow1 <- rep(NA, N)
    fow1[!is.na(arm)] <- sample(c("Withdraw", "Discontinued", "Death", "Other", NA),
                                sum(!is.na(arm)), replace = T, 
                                prob = c(0.05, 0.05, 0.05, 0.05, 0.8))
    fow2 <- rep(NA, N)
    fow2[!is.na(arm) & is.na(fow1)] <- sample(c("Protocol deviation", "Outcome missing", NA),
                                              sum(!is.na(arm) & is.na(fow1)), replace = T, 
                                              prob = c(0.05, 0.05, 0.9))
    df <- data.frame(trialno, exc, arm, fow1, fow2)
    head(df)
    #>   trialno  exc  arm  fow1 fow2
    #> 1    1086 <NA> Conc  <NA> <NA>
    #> 2    1418 <NA>  Seq  <NA> <NA>
    #> 3    1502 <NA> Conc Death <NA>
    #> 4    1846 <NA> Conc  <NA> <NA>
    #> 5    1303 <NA> Conc Death <NA>
    #> 6    1838 <NA>  Seq  <NA> <NA>
    
    out <- consort_plot(data = df,
                        order = c(trialno = "Population",
                                  exc    = "Excluded",
                                  arm     = "Randomized patient",
                                  fow1    = "Lost of Follow-up",
                                  trialno = "Finished Followup",
                                  fow2    = "Not evaluable",
                                  trialno = "Final Analysis"),
                        side_box = c("exc", "fow1", "fow2"),
                        allocation = "arm",
                        labels = c("1" = "Screening", "2" = "Randomization",
                                   "5" = "Final"),
                        cex = 0.6)
    
    plot(out)
    grid.text("SOMETHING NICE AND BIG", x = 0.3, y = 0.9, gp = gpar(col = "red", fontsize = 18))
    

    Created on 2024-05-23 with reprex v2.1.0