如何更改或交换 R 中堆积条形图的颜色?
How to change or swap the color for a stacked bar plot in R?
我有一个这样的矩阵:
my.matrix:
A B C D E F G H
[1,] 12.1 8.10 7.79 11.40 10.30 15.10 9.88 13.90
[2,] 0.0 5.45 0.00 0.00 0.00 0.00 0.00 0.00
[3,] 0.0 0.00 5.42 0.00 0.00 0.00 0.00 0.00
[4,] 0.0 0.00 0.00 6.55 0.00 0.00 0.00 0.00
[5,] 0.0 0.00 0.00 0.00 4.68 0.00 0.00 0.00
[6,] 0.0 0.00 0.00 0.00 0.00 4.55 0.00 0.00
[7,] 0.0 0.00 0.00 0.00 0.00 0.00 4.32 0.00
[8,] 0.0 0.00 0.00 0.00 0.00 0.00 0.00 3.94
并且我生成了条形图:
barplot((my.matrix), beside=F, axis.lty=1, xpd=T,
ylim= c(0,30),xlim=c(0,11), horiz=F,yaxt='n',
axisnames=F,
col=c("black","darkolivegreen1","steelblue2","hotpink3","lightpink","gold","darkslategray1","peachpuff"))
剧情如下:
所以,我想要的是将各个列的颜色从第二列交换到最后一列。
例如,在第 2 列中,您可以看到黑色之上是绿色;
但我希望黑色超过绿色,其余列也相同,同时保持矩阵的顺序相同。
这是我用 photoshop 为前三列着色的示例:
我试图反转或转置矩阵,但它不起作用,我有点卡在这部分了。
如果你能帮我解决这个问题,我将不胜感激!
最佳,
你可以做到
barplot(my.matrix[nrow(my.matrix):1, ], beside=F, axis.lty=1, xpd=T,
ylim= c(0,30),xlim=c(0,11), horiz=F,yaxt='n',
axisnames=F,
col=c("peachpuff", "darkslategray1", "gold", "lightpink", "hotpink3",
"steelblue2", "darkolivegreen1", "black"))
或者,关于您的编辑:
m <- my.matrix
diag(m) <- my.matrix[1, ]
m[1, ] <- diag(my.matrix)
barplot(m[nrow(m):1, ], beside=F, axis.lty=1, xpd=T,
ylim= c(0,30),xlim=c(0,11), horiz=F,yaxt='n',
axisnames=F,
col=c("peachpuff", "darkslategray1", "gold", "lightpink", "hotpink3",
"steelblue2", "darkolivegreen1", "black"))
数据:
my.matrix <- read.table(header=T, text=" A B C D E F G H
[1,] 12.1 8.10 7.79 11.40 10.30 15.10 9.88 13.90
[2,] 0.0 5.45 0.00 0.00 0.00 0.00 0.00 0.00
[3,] 0.0 0.00 5.42 0.00 0.00 0.00 0.00 0.00
[4,] 0.0 0.00 0.00 6.55 0.00 0.00 0.00 0.00
[5,] 0.0 0.00 0.00 0.00 4.68 0.00 0.00 0.00
[6,] 0.0 0.00 0.00 0.00 0.00 4.55 0.00 0.00
[7,] 0.0 0.00 0.00 0.00 0.00 0.00 4.32 0.00
[8,] 0.0 0.00 0.00 0.00 0.00 0.00 0.00 3.94")
my.matrix <- as.matrix(my.matrix)
我有一个这样的矩阵:
my.matrix:
A B C D E F G H
[1,] 12.1 8.10 7.79 11.40 10.30 15.10 9.88 13.90
[2,] 0.0 5.45 0.00 0.00 0.00 0.00 0.00 0.00
[3,] 0.0 0.00 5.42 0.00 0.00 0.00 0.00 0.00
[4,] 0.0 0.00 0.00 6.55 0.00 0.00 0.00 0.00
[5,] 0.0 0.00 0.00 0.00 4.68 0.00 0.00 0.00
[6,] 0.0 0.00 0.00 0.00 0.00 4.55 0.00 0.00
[7,] 0.0 0.00 0.00 0.00 0.00 0.00 4.32 0.00
[8,] 0.0 0.00 0.00 0.00 0.00 0.00 0.00 3.94
并且我生成了条形图:
barplot((my.matrix), beside=F, axis.lty=1, xpd=T,
ylim= c(0,30),xlim=c(0,11), horiz=F,yaxt='n',
axisnames=F,
col=c("black","darkolivegreen1","steelblue2","hotpink3","lightpink","gold","darkslategray1","peachpuff"))
剧情如下:
所以,我想要的是将各个列的颜色从第二列交换到最后一列。
例如,在第 2 列中,您可以看到黑色之上是绿色; 但我希望黑色超过绿色,其余列也相同,同时保持矩阵的顺序相同。
这是我用 photoshop 为前三列着色的示例:
我试图反转或转置矩阵,但它不起作用,我有点卡在这部分了。
如果你能帮我解决这个问题,我将不胜感激!
最佳,
你可以做到
barplot(my.matrix[nrow(my.matrix):1, ], beside=F, axis.lty=1, xpd=T,
ylim= c(0,30),xlim=c(0,11), horiz=F,yaxt='n',
axisnames=F,
col=c("peachpuff", "darkslategray1", "gold", "lightpink", "hotpink3",
"steelblue2", "darkolivegreen1", "black"))
或者,关于您的编辑:
m <- my.matrix
diag(m) <- my.matrix[1, ]
m[1, ] <- diag(my.matrix)
barplot(m[nrow(m):1, ], beside=F, axis.lty=1, xpd=T,
ylim= c(0,30),xlim=c(0,11), horiz=F,yaxt='n',
axisnames=F,
col=c("peachpuff", "darkslategray1", "gold", "lightpink", "hotpink3",
"steelblue2", "darkolivegreen1", "black"))
数据:
my.matrix <- read.table(header=T, text=" A B C D E F G H
[1,] 12.1 8.10 7.79 11.40 10.30 15.10 9.88 13.90
[2,] 0.0 5.45 0.00 0.00 0.00 0.00 0.00 0.00
[3,] 0.0 0.00 5.42 0.00 0.00 0.00 0.00 0.00
[4,] 0.0 0.00 0.00 6.55 0.00 0.00 0.00 0.00
[5,] 0.0 0.00 0.00 0.00 4.68 0.00 0.00 0.00
[6,] 0.0 0.00 0.00 0.00 0.00 4.55 0.00 0.00
[7,] 0.0 0.00 0.00 0.00 0.00 0.00 4.32 0.00
[8,] 0.0 0.00 0.00 0.00 0.00 0.00 0.00 3.94")
my.matrix <- as.matrix(my.matrix)