Wednesday, 15 February 2012

bar chart - Separate Condition based coloring of different columns in bar-plot in R -



bar chart - Separate Condition based coloring of different columns in bar-plot in R -

i have next info of particular candidates assessment 1 time again benchmark data. find csv file here:

competencies desired score drive 6 6.72 cust orientation 6 6.58 innovation 6 6.43 team work 5 6.88 analytical thinking 6 7 leadership 3 6.42 assertiveness 3 6.73 problem solving 4 6.73 implementation & execution 6 6.85 working knowledge 6 5 bu knowledge 3 4.58 fu knowledge 3 4.7 knowledge of business environment 4 4.72

i have create horizontal bar plot this:

http://i.stack.imgur.com/jleqf.png

where color of bench mark column fixed bluish , color of score column based on next condition:

cols <- ifelse(import1$score>import1$desired,"green", ifelse(import1$score>=(0.96*import1$desired) & import1$score<import1$desired, "yellow", "red"))

how do in bar plot barplot takes colors mentioned below (i have manually input it):

barplot(t(as.matrix(import1[,1:2])),horiz=true, col=c("blue","green","blue", "green","blue", "green","blue", "green","blue","green","blue", "green","blue","green" ,"blue", "green","blue", "green","blue", "red","blue","green","blue", "green","blue", "green" ),cex.names=0.5,las=1,cex.axis=0.6,beside=true,border=na)

i want status based approach in col.

edit: additional query

if have multiple scores same set of competencies, how create separate set of charts on above mentioned condition.

competencies desired score 1 score 2 score 3 drive 6 6.72 5.2 6.6 cust orientation 6 6.58 6 7.6 innovation 6 6.43 4.2 7 team work 5 6.88 5.4 7.8 analytical thinking 6 7 4.6 7 leadership 3 6.42 5.8 7 assertiveness 3 6.73 4.8 6.4 problem solving 4 6.73 6 6.6 implementation & execution 6 6.85 6.2 6 working knowledge 6 5 3.6 5.4 bu knowledge 3 4.58 3.8 4.4 fu knowledge 3 4.7 4 4.6 knowledge of business environment 4 4.72 4 4.8

since have colors want in cols can create vector want first generating plenty "blue" values this:

blues <- rep("blue",length(cols))

and combine vector other vector colors want using:

colors <- as.vector(rbind(blues,cols))

after plot can created by:

barplot(t(as.matrix(import1[,2:3])),horiz=true, col=colors,cex.names=0.5,las=1,cex.axis=0.6,beside=true,border=na)

edit:

combining of function looks this:

calccols <- function(df,score) { cols <- ifelse(df[,score]>df$desired,"green", ifelse(df[,score]>=(0.96*df$desired) & df[,score]<df$desired, "yellow", "red")) blues <- rep("blue",length(cols)) as.vector(rbind(blues,cols)) }

after can create 3 charts want new info using:

barplot(t(as.matrix(import1[,2:3])),horiz=true, col=calccols(import1,"score.1"),cex.names=0.5,las=1,cex.axis=0.6,beside=true,border=na) barplot(t(as.matrix(import1[,2:3])),horiz=true, col=calccols(import1,"score.2"),cex.names=0.5,las=1,cex.axis=0.6,beside=true,border=na) barplot(t(as.matrix(import1[,2:3])),horiz=true, col=calccols(import1,"score.3"),cex.names=0.5,las=1,cex.axis=0.6,beside=true,border=na)

r bar-chart

No comments:

Post a Comment