rplot

How to use the cols parameter to change the color of certain data points in the albatros function of the metap package?


I would like to create an albatros plot in R Studio, so I downloaded the metap package (https://www.rdocumentation.org/packages/metap/versions/1.11).

With the help of the description and sample code, I was able to produce a plot with my own data, see below. The studies I want to analyze used different methods, and I would like to display those in different colors. E.g. Method 1 has red Datapoints, Method 2 has blue, Method 3 has green.

I cannot figure out how to make the cols parameter work, every time I input something other than NULL either the code just does not run or I get error messages. Could you guys help me out?

This is the description of the cols parameter:

The pch parameter may either be a single value or a vector of the same length as p. It may contain anything which is legal as input to the parameter of the same name in the graphics library and will be used for the plotted points. The cols parameter works similarly.

Plot:

enter image description here

My code:

albatros(Test_Albatross$p, Test_Albatross$n,
                  contours = list(type = "smd", contvals = c(0.25, 0.5, 0.8), ltys = 1:3),
                  axes = list(ylimit = c(1,100), xlimit = 0.0001,  lefttext = "Negative correlation",
                              righttext = "Positive correlation"),
                  plotpars = list(yscale = NULL, pchs = letters[unclass(Test_Albatross$Kennung)], cols = NULL),
main = "Test2")

My Dataset (Excel):

enter image description here

I already tried:

cols = colors[unclass(Test_Albatross$Method)]
cols = "red"

rgb, hex, values, ...


Solution

  • According to the help page of albatros

    > help(albatros)
    

    plotpars A list containing

    ... Arguments to be passed through to plot


    This says the plotpars argument should contain only 3 plotting parameters (yscale, pch, and col) and any other plotting parameters are to be specified in .... However, cols is not being passed to plot, and this looks like a bug.

    albatros(Test_Albatross$p, Test_Albatross$n,
             contours = list(type = "smd", contvals = c(0.25, 0.5, 0.8), ltys = 1:3),
             axes = list(ylimit = c(1,100), xlimit = 0.0001,  lefttext = "Negative correlation",
                         righttext = "Positive correlation"),
             plotpars = list(yscale = NULL, 
                             pchs = letters[Test_Albatross$Kennung], 
                             cols = rainbow(10)))
    

    enter image description here


    Interestingly, the following does change the colours of the labels. Unfortunately, the colour of the axes is now linked to the col global argument, and if this is specified, then that will be the colour of the axes. If a vector is specified, then the colour will be the first colour. A trick is to specify the first colour as black. However, the first label ("a") will also have to be black.

    albatros(Test_Albatross$p, Test_Albatross$n,
             contours = list(type = "smd", contvals = c(0.25, 0.5, 0.8), ltys = 1:3),
             axes = list(ylimit = c(1,100), xlimit = 0.0001,  lefttext = "Negative correlation",
                         righttext = "Positive correlation"),
             plotpars = list(yscale = NULL, 
                             pchs = letters[Test_Albatross$Kennung]),
             col = c("black", rainbow(9)))
    

    enter image description here


    If you don't want the first label to be black, then there is another option. But it is rather tedious. Run the command again with a full rainbow of colours.

    albatros(Test_Albatross$p, Test_Albatross$n,
             contours = list(type = "smd", contvals = c(0.25, 0.5, 0.8), ltys = 1:3),
             axes = list(ylimit = c(1,100), xlimit = 0.0001,  lefttext = "Negative correlation",
                         righttext = "Positive correlation"),
             plotpars = list(yscale = NULL, 
                             pchs = letters[Test_Albatross$Kennung]),
             col = rainbow(10))
    

    You get coloured labels, even the first one. But the axes are red (the first colour of the rainbow). We then simply copy the albatros code that constructs the axes and plot these ourselves over the existing plot.

    xtrans <- function(p) {
      p2 <- ifelse(p > 0.5, 1 - p, p)
      res <- log(1/p2, base = 10)
      res <- ifelse(p > 0.5, -res, res)
      res
    }
    
    sigs <- c(0.050, 0.010, 0.001)
    i <- 2
    while (sigs[i] > 0.001) {
      sigs[i + 1] <- sigs[i]/10
      i <- i + 1
    }
    
    ylabs <- c(1, 2, 5)
    maxn <- max(Test_Albatross$n)
    i <- 3
    while (ylabs[i] < maxn) {
      ylabs[i + 1] <- ylabs[i - 2] * 10
      i <- i + 1
    }
    
    axis(1, at = c(-xtrans(sigs), 0, xtrans(sigs)), labels = c(sigs, "null", sigs),
         cex.axis = 0.75, las = 3)
    axis(2, at=sqrt(ylabs), labels = ylabs, cex.axis = 0.75, las = 2)
    

    enter image description here