I have two data frames and a function maxthree which takes in a data frame and row name as an argument and plots the three highest value on the row (in a descending order) and the name of the column of three highest value.
set.seed(0)
df <- data.frame(A=c(3,2,1,4,5),B=c(1,6,3,8,4),C=c(2,1,4,8,9), D=c(4,1,2,4,6))
row.names(df)<-c("R1","R2","R3","R4","R5")
df2 <- data.frame(E=c(2,5,6,1,4),F=c(2,4,2,5,1),G=c(5,6,2,7,3),H=c(8,2,7,4,1))
row.names(df2)<-c("R6","R7","R8","R9","R10")
print(df)
A B C D
R1 3 1 2 4
R2 2 6 1 1
R3 1 3 4 2
R4 4 8 8 4
R5 5 4 9 6
print(df2)
E F G H
R6 2 2 5 8
R7 5 4 6 2
R8 6 2 2 7
R9 1 5 7 4
R10 4 1 3 1
maxthree <- function(data, row) {
order<-as.matrix(data[row, order(unlist(data[row, ]), decreasing = TRUE)[1:3]])
barplot(order)
}
maxthree(df2, "R7")
I am trying to have the function maxthree to color each bar differently depending on the column they would be on. So, for example, if columns in df2 would be colored in a following way: E=green, F=red, G=yellow, H=blue, the bars in the previous bar graph should be yellow, green, red (in that order). I know how to color in a normal bar plot by using "col" inside barplot() but I don't know how to color in the bars in this situation since it is dependent on which columns appear on the bar plot and on the order of them.