更改堆叠条形图 R 中各个条形图的颜色

Changing colours of individual bars in a stacked barplot R

我有不同细胞系的感染水平数据(按严重程度分为 1-3 级)。我创建了一个堆叠条形图,但当我想按颜色分隔线条时,我只能更改所有条形的颜色。我希望 2 WT 有红色调色板,2 KO 有黄色调色板,AB 线有蓝色调色板。

我的数据是这样的:

我试过了,但它给了我下面的图表:

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)]

如有任何帮助,我们将不胜感激!

谢谢:)

编辑输出(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))

或许您可以单独为条形图着色。我从这个 post here.

中想到了一些想法

将使 cols 成为一个包含不同条形图的不同调色板的矩阵。

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)
}