1

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.

Andrea Ianni
  • 829
  • 12
  • 24
  • 2
    Did you look at the example in `plot.kohonen` where they use `pallette.name`? It is a function. Specifically, in that help page, there is a function called `coolBlueHotRed` that gets passed to `pallette.name`. This works fine for `type = "counts"`. – David May 09 '16 at 23:42
  • Yes, at the end I've used it. Nevertheless, I would have liked to change the colors but I haven't understood how. For example the scale of colors on red. coolBlueHotRed is a function Maybe I should write a function that produces the colors I want. I'll see. In the meantime, thanks for your comment. – Andrea Ianni May 10 '16 at 06:40
  • 1
    In the function `coolBlueHotRed`, change `rainbow` to `heat.colors` instead. That will give you a series of red to orange to yellow range of colors. – David May 19 '16 at 02:52
  • 1
    I should have added that there isn't really a built-in palette of reds, and `heat.colors()` has the most reds available. You can make custom sets of colors but you will have to create the range of colors yourself. Functions like `rgb()` and `hsv()` translate colors in RGB or HSV formats to hex names, which can be used to specify your custom colors. Also look at `colors()` for the built-in array of colors that R knows. – David May 19 '16 at 03:08
  • 1
    Thanks, it works perfectly. I think you should insert it as an answer! – Andrea Ianni May 19 '16 at 07:04
  • Linked also with the following question https://stackoverflow.com/questions/25965167/error-plotting-kohonen-maps-in-r – Nemesi Nov 30 '18 at 11:06

1 Answers1

0

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

Kai
  • 28
  • 3