Reputation: 25
I have data on infection levels (graded 1-3 by severity) in different cell lines. I have created a stacked barplot but I can only change the colours of all the bars, when I'd like to separate lines by colour. I'd like the 2 WT to have a red palette, the 2 KO a yellow palette and the AB lines a blue palette.
My data looks like this:
I tried this but it gives me the following graph:
inf.level<-read.csv("Infection_level.csv")
cols<-c("darkred", "red", "darksalmon", "darkorange3", "darkorange", "orange", "blue4", "blue", "cornflowerblue")
barplot(as.matrix(inf.level), ylab="Tsetse infection (%)", col=cols)]
Any help would be appreciated!
Thank you :)
EDIT dput(inf.level) :
structure(list(WT_MG = c(29.41176471, 23.52941176, 11.76470588 ), WT_PV = c(21.42857143, 7.142857143, 14.28571429), KO_MG = c(5.555555556, 16.66666667, 33.33333333), KO_PV = c(0L, 0L, 0L), AB_MG = c(0, 28.57142857, 4.761904762), AB_PV = c(0, 0, 5.555555556)), .Names = c("WT_MG", "WT_PV", "KO_MG", "KO_PV", "AB_MG", "AB_PV"), class = "data.frame", row.names = c(NA, -3L))
Upvotes: 2
Views: 92
Reputation: 30474
Perhaps you can color the bars individually. I came across a few ideas from this post here.
Would make cols
a matrix that contains the different color palettes for different bars.
cols<-matrix(c(rep(c("darkred", "red", "darksalmon"), 2),
rep(c("darkorange3", "darkorange", "orange"), 2),
rep(c("blue4", "blue", "cornflowerblue"), 2)),
ncol = 6, nrow = 3)
inf.level.mat <- as.matrix(inf.level)
barplot(inf.level.mat, ylab="Tsetse infection (%)")
for (i in 1:ncol(inf.level.mat)) {
x = inf.level.mat
x[,-i] <- NA
barplot(x, col = cols[,i], add=TRUE)
}
Upvotes: 1