agrajag_42
agrajag_42

Reputation: 165

Column colouring in R

I have the following code to produce a barchart in R:

library(lattice)
options(scipen = 10)

barchart(Cost~KPI, data=lowD, groups = Sequence, 
         scales=list(x=list(rot=90,cex=0.8)),
         xlab="Performance Indicators", ylab="Timetable Cost (£)",
         ylim = c(0,50000),
         col=c("aquamarine","coral"))

As you can see from the attachment though, each column is further divided into 3 pieces. Does anyone have any idea what these 3 pieces represent? I though that they indicated the amount attributed to each passenger type but it seems that I am mistaken. Ideally, I would like to divide each column into 3 pieces to indicate the contribution of each passenger type, paint each piece a different color and add a legend.

The input I am using is the following:

**PassengerType**   **Cost**        **KPI**        **CurrOpt**
  Business              0          Crowdedness       Current
  Commute               0          Crowdedness       Current
  Leisure               0          Crowdedness       Current
  Business            41349        Journey Time      Current
  Commute             18247        Journey Time      Current
  Leisure              5050        Journey Time      Current
  Business            36654        Punctuality       Current
  Commute             16418        Punctuality       Current
  Leisure              4470        Punctuality       Current
  Business            10496        Waiting Time      Current
  Commute              4800        Waiting Time      Current
  Leisure              1280        Waiting Time      Current
  Business               0         Crowdedness       Optimal
  Commute                0         Crowdedness       Optimal
  Leisure                0         Crowdedness       Optimal
  Business             42642       Journey Time      Optimal
  Commute              18839       Journey Time      Optimal
  Leisure               5208       Journey Time      Optimal
  Business             25319       Punctuality       Optimal
  Commute              11258       Punctuality       Optimal
  Leisure               3087       Punctuality       Optimal
  Business              9731       Waiting Time      Optimal
  Commute               4536       Waiting Time      Optimal
  Leisure               1184       Waiting Time      Optimal

Bar Plot

Upvotes: 1

Views: 68

Answers (1)

Miha
Miha

Reputation: 2884

As you didn't add your data frame it is not entirely clear what you want.

But one of options to color different sequences or values inside bar would be to:

# Create data
vector <- seq(1:4)
mat.1    <- matrix(sample(vector, 40, replace = T), nrow=4, byrow = T)
mat.2   <-  matrix(rep(1,40), nrow=4, byrow=T)

# Define conditions and colors under the condition. So you might use something like that: 
cols <- ifelse((mat.1 ==1) , "blue",
ifelse( (mat.1 ==2), "yellow",
ifelse( (mat.1 ==3) , "skyblue",
ifelse((mat.1 ==4), "red", "purple" ))))

barplot(mat.2,col=cols, width = c(0.2),xlim = c(0,4),beside=F)

And the output (point here are different colors in bars based on values) enter image description here

Upvotes: 1

Related Questions