rplotcolorssomself-organizing-maps

Plotting a SOM: how to change the colours?


I have the following dataset.

> ds
          dataset_clustering.CODICE_DOMANDA  FAT_AZI_MLN IMPORTO_PROGETTO NUM_ADDETTI Modelli Formazione
    22870                             22870 4.855849e-03       0.25386313 0.001403368       0          0
    22893                             22893 3.606493e-04       0.40618102 0.115209837       0          0
    16258                             16258 4.433197e-04       0.58254746 0.140670944       0          0
    14684                             14684 6.189941e-04       0.35717439 0.304330393       0          0
    12873                             12873 2.480110e-05       0.65121413 0.022654370       0          0
    12933                             12933 2.603806e-02       0.02551876 0.017107725       0          1
    12047                             12047 7.130316e-05       0.20094923 0.005012029       0          0
    22880                             22880 1.963420e-05       0.16556291 0.015503876       0          0
    11479                             11479 2.856260e-03       0.57615894 0.786688051       0          1
    20836                             20836 3.089804e-04       0.20640177 0.004611067       0          0
          Investimento Sostituzione CAMPANIA COSTRUZIONI ESITO
    22870            1            0        1           1     B
    22893            1            0        1           1     R
    16258            1            0        1           1     P
    14684            1            0        1           1     P
    12873            1            0        1           1     B
    12933            0            0        1           1     B
    12047            1            0        1           1     R
    22880            1            0        1           1     B
    11479            0            0        1           1     P
    20836            1            0        1           1     B

I want to train a SOM that cluster that dataset:

# Train a SOM
grid_type<-'hexagonal' # 'rectangular'
library(kohonen)
num_rows <- 3
num_cols <- 3
r_length <- 100 # 100 1000 10000 100000
tpsom<-paste(grid_type," (",num_rows,"x",num_cols,")")
set.seed(13)
#-------------------------------------------------------------------
somres <- som(as.matrix(ds[,2:length(colnames(ds))-1]),
              grid=somgrid(ydim=num_rows,xdim=num_cols,grid_type),
              rlen = r_length # default = 100
)
# Create a plot of the som
png("Example.png")
  par(mar=c(5.1,4.1,6.1,2.1))
  plot(somres,
       type = "counts",
       #main=paste("Analisi SOM - ","Numero di input per cella","\n")
       main=paste("Analisi SOM\n","Numero di input per cella","\n",
                  "Mappa: ",tpsom
       )
  )
dev.off()

If I do that I obtain the following figure: enter image description here

I don't like how it automatically set the colours, because it seems counterintuitive. I'd like to set five colours, starting from white and becoming light red, normal red, dark red, darker red. Otherwise, at least invert the colours of the figure.

I cannot use the parameter palette.name= etc etc. How can I modify the colours? If I use ggplot I loose the property type = "counts" that I have plotting the som.


Solution

  • have you considered using 'function' to create your own colour palettes? You can change them (n, alpha, rev = True/False) depending on the code below:

     customisedcolors <- function(n, alpha = 1) {
      heat.colors(n, alpha=alpha)[n:1]
    }
    

    Best wishes, Kai