Search code examples
rcolorsstackedbarseries

Attribute colors by row in a barplot


I have trouble with the attributing colors in barplot to rows, they are non-existent. When I use the command:

barplot(data[,3:429])

it does give me the correct pattern in terms of height, but it is all black. I have 22 different rows (each with 427 variables), but those do not identify in my plot.

A small part of the data looks like this:

VT  ATC V4  V5  V6  V7  V8  V9  V10 V11 V12 .... V429
1   1   0   1   1   2   2   2   2   1   1
1   2   0   1   1   1   1   1   1   1   1
1   3   0   0   0   0   0   0   0   0   0
1   4   0   0   0   0   0   0   0   0   0
1   5   0   0   0   1   1   1   1   0   0

I've tried adding colors, but it doesn't work like this:

barplot(data[,3:429],col=c("blue", "aliceblue","antiquewhite", "antiquewhite1","antiquewhite2","antiquewhite3", "antiquewhite4","aquamarine","aquamarine1","aquamarine2", "aquamarine3", "aquamarine4", "darkgreen", "darkgrey","darkkhaki","darkmagenta", "darkolivegreen","darkolivegreen1","darkolivegreen2","darkolivegreen3","darkolivegreen4","darkorange"))

The 22 colors were chosen randomly, there is no specific meaning to them.

What am I doing wrong?


Solution

  • Following works for me:

    #dummy data
    m <- matrix(sample(1:10,500*22,replace=TRUE),ncol=500)
    
    #stacked bar plot with colours
    barplot(m[,3:400],
            col=c("blue", "aliceblue","antiquewhite", "antiquewhite1","antiquewhite2","antiquewhite3", "antiquewhite4","aquamarine","aquamarine1","aquamarine2", "aquamarine3", "aquamarine4", "darkgreen", "darkgrey","darkkhaki","darkmagenta", "darkolivegreen","darkolivegreen1","darkolivegreen2","darkolivegreen3","darkolivegreen4","darkorange"),
            border = FALSE, space=0
            )
    

    Is this what you expect to get?

    enter image description here