Search code examples
rplotcategorical-datalongitudinal

Personalize colors in lasagna plot using longCatEDA


A lasagna plot with 3 categorical variables can be plot using this code and works fine

[![library(longCatEDA)
library(colorspace)
library(RColorBrewer)
set.seed(642531)
y <- matrix(sample(1:3, 500, replace=TRUE), 100, 5)
set.seed(963854)
times <- matrix(runif(600, 1, 3), 100, 6)

# times must be cumulative
times <- t(apply(times, 1, cumsum))
lc <- longCat(y, times=times)

par(mfrow=c(1,1), bg='white', mar=c(5.1, 4.1, 4.1, 10.1), xpd=TRUE)


cols <- longCatPlot(lc,  colScheme='rainbow', legendBuffer=0, groupBuffer=0,main='How to personalize colors?')

legend(16.5, 100, legend=lc$Labels, lty=1, col=cols, lwd=2)][1]][1]

enter image description here

But I'm trying to personalize the colors:

1.Red

2.Yellow

3.Green

I could not find how to use specific colors, so I tried the default palettes and the best I could do was using the "rainbow" scheme. There are also the colorspace and the RColorBrewer but none of them includes the 3 colors I need (red, yellow and green in that order)


Solution

  • Instead of using the colScheme-argument use cols instead:

    library(longCatEDA)
    set.seed(642531)
    y <- matrix(sample(1:3, 500, replace=TRUE), 100, 5)
    set.seed(963854)
    times <- matrix(runif(600, 1, 3), 100, 6)
    
    # times must be cumulative
    times <- t(apply(times, 1, cumsum))
    lc <- longCat(y, times=times)
    
    par(mfrow=c(1,1), bg='white', mar=c(5.1, 4.1, 4.1, 10.1), xpd=TRUE)
    
    cols <- longCatPlot(lc, cols = c("red", "yellow", "green"), legendBuffer=0, groupBuffer=0,main='How to personalize colors?')
    
    legend(16.5, 100, legend=lc$Labels, lty=1, col=c("red", "yellow", "green"), lwd=2)
    

    Created on 2021-08-17 by the reprex package (v2.0.1)