r grouped barplot - columns in groups shift in one function but not another?

Hi, the order of months in below plot shifts by one month going from group "a" (starts Jan) to "b" (starts Feb) to "c". (starts Mar).

require(RColorBrewer)
require(lubridate)
data <- runif(36, 1,6)
info<-matrix(data,12,3)
print(info)
rownames(info)<-factor(month(1:12), levels=1:12, labels=month.abb, ordered=TRUE)
colnames(info)<-c("a","b","c")
barplot(info,beside=T,col=brewer.pal(n = 12, name = "RdBu"),legend=rownames(info),args.legend = list(x="bottomright", inset = c(-0.25, 0)))

I am puzzled why this is happening as I've another similar example without months whether each group is in same order.

data <- structure(list(W= c(1L, 3L, 6L, 4L, 9L), X = c(2L, 5L,
4L, 5L, 12L), Y = c(4L, 4L, 6L, 6L, 16L), Z = c(3L, 5L,
6L, 7L, 6L)), .Names = c("W", "X", "Y", "Z"), class = "data.frame", row.names = c(NA, -5L))
attach(data)
print(data)
colours <- c("red", "orange", "blue", "yellow", "green")
barplot(as.matrix(data), main="My Barchart", ylab = "Numbers", cex.lab = 1.5, cex.main = 1.4, beside=TRUE, col=colours)
legend("topleft", c("First","Second","Third","Fourth","Fifth"), cex=1.3, bty="n", fill=colours)

(source: https://www.theanalysisfactor.com/r-11-bar-charts/)

Please tell me why first R code leads to a shifting of the columns in each group whereas in the second example it doesnt?

I'm not totally clear on what your question is, but here's a reprex with the code you've included, which will make it easier to discuss:

require(RColorBrewer)
#> Loading required package: RColorBrewer
require(lubridate)
#> Loading required package: lubridate
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date
data <- runif(36, 1,6)
info<-matrix(data,12,3)
print(info)
#>           [,1]     [,2]     [,3]
#>  [1,] 4.366595 4.755404 1.475025
#>  [2,] 4.301224 5.630490 5.581186
#>  [3,] 2.095352 1.292955 2.376578
#>  [4,] 5.452962 2.902681 1.325580
#>  [5,] 3.914498 3.961479 1.012992
#>  [6,] 5.125957 4.539389 2.524167
#>  [7,] 3.712348 1.566362 4.543569
#>  [8,] 4.944474 1.793858 1.040954
#>  [9,] 3.598981 5.625973 1.469789
#> [10,] 2.468821 5.389284 3.116372
#> [11,] 3.506745 4.597780 5.504062
#> [12,] 1.605065 2.077325 4.244833
rownames(info)<-factor(month(1:12), levels=1:12, labels=month.abb, ordered=TRUE)
colnames(info)<-c("a","b","c")
barplot(info,beside=T,col=brewer.pal(n = 12, name = "RdBu"),legend=rownames(info),args.legend = list(x="bottomright", inset = c(-0.25, 0)))
#> Warning in brewer.pal(n = 12, name = "RdBu"): n too large, allowed maximum for palette RdBu is 11
#> Returning the palette you asked for with that many colors


data <- structure(list(W= c(1L, 3L, 6L, 4L, 9L), X = c(2L, 5L,
                                                       4L, 5L, 12L), Y = c(4L, 4L, 6L, 6L, 16L), Z = c(3L, 5L,
                                                                                                       6L, 7L, 6L)), .Names = c("W", "X", "Y", "Z"), class = "data.frame", row.names = c(NA, -5L))
attach(data)
print(data)
#>   W  X  Y Z
#> 1 1  2  4 3
#> 2 3  5  4 5
#> 3 6  4  6 6
#> 4 4  5  6 7
#> 5 9 12 16 6
colours <- c("red", "orange", "blue", "yellow", "green")
barplot(as.matrix(data), main="My Barchart", ylab = "Numbers", cex.lab = 1.5, cex.main = 1.4, beside=TRUE, col=colours)
legend("topleft", c("First","Second","Third","Fourth","Fifth"), cex=1.3, bty="n", fill=colours)

Created on 2019-06-11 by the reprex package (v0.3.0)

Oh! The issue in the first one is that the palette you're using has fewer colours than the groups you're plotting

Warning message:
In brewer.pal(n = 12, name = "RdBu") :
  n too large, allowed maximum for palette RdBu is 11
Returning the palette you asked for with that many colors

So, it's recycling the colours, and, thus, shifting which goes first.

1 Like

Hi, many thanks for your help.

If you look at the first barplot the first column of each of the groups a, b, c are dark red then mid red then orange. The legend isnt showing but this corresponds to group "a" starting with Jan, group "b" starting with Feb and group "c" starting with March. I want each group to start with Jan and not to shift by one month. I tried just using number 1:12 as rownames but got same shifting.

Strangely in the sourced example (which looks same stcucture to me), the second chart shows each group starting with "First" ie colour red.

Is that any clearer why I am so puzzled?

The difference between the top and bottom examples is that you're applying different colour schemes, and have a different number of elements. You can expand the number of colours in a ColorBrewer palette using colorRampPalette().

library(RColorBrewer)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date
data <- runif(36, 1, 6)
info <- matrix(data, 12, 3)
print(info)
#>           [,1]     [,2]     [,3]
#>  [1,] 1.381053 2.456754 4.358200
#>  [2,] 1.338623 2.218607 5.182006
#>  [3,] 1.595358 4.131486 1.053117
#>  [4,] 3.228294 5.348968 5.969906
#>  [5,] 1.456573 3.548517 2.927227
#>  [6,] 5.464193 3.148610 5.932161
#>  [7,] 5.893941 3.376215 5.366045
#>  [8,] 2.804159 4.684815 2.539839
#>  [9,] 5.144978 4.647492 5.029717
#> [10,] 1.532950 5.218644 5.635092
#> [11,] 4.546222 5.272840 5.059196
#> [12,] 3.090123 3.802192 5.482178
rownames(info) <- factor(month(1:12), levels = 1:12, labels = month.abb, ordered = TRUE)
colnames(info) <- c("a", "b", "c")
print(info)
#>            a        b        c
#> Jan 1.381053 2.456754 4.358200
#> Feb 1.338623 2.218607 5.182006
#> Mar 1.595358 4.131486 1.053117
#> Apr 3.228294 5.348968 5.969906
#> May 1.456573 3.548517 2.927227
#> Jun 5.464193 3.148610 5.932161
#> Jul 5.893941 3.376215 5.366045
#> Aug 2.804159 4.684815 2.539839
#> Sep 5.144978 4.647492 5.029717
#> Oct 1.532950 5.218644 5.635092
#> Nov 4.546222 5.272840 5.059196
#> Dec 3.090123 3.802192 5.482178
coul <- brewer.pal(n = 12, name = "Set1")
#> Warning in brewer.pal(n = 12, name = "Set1"): n too large, allowed maximum for palette Set1 is 9
#> Returning the palette you asked for with that many colors
coul <- colorRampPalette(coul)(12)
barplot(info, beside = T, col = coul, legend = rownames(info), args.legend = list(x = "bottomright", inset = c(-0.25, 0)))

Created on 2019-06-11 by the reprex package (v0.3.0)

Thanks so much. I mistakenly thought the palette would handle 12 colours!

Am now using for my palette the pals package: tols(n=12)

Thanks again

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.