-1

堆积条形图始终按填充对同一条形图上的段进行排序。如何避免这种情况?我想要实现的是一个堆叠的条形图,带有交替的蓝色和红色段(代表状态转换)

4

1 回答 1

1

Base graphics solution

Use the col argument of barplot:

d <- rep(1, 4)
d <- as.matrix(d, nrow=4, ncol=1)
barplot(d, beside=FALSE, col=c("red", "blue", "red", "blue"))

enter image description here

ggplot solution

require(ggplot2)
redBlue <- rep(c("red", "blue"), length(levels(diamonds$color))) 
#redBlue is twice as long as necessary, but that's harmless

ggplot(diamonds, aes(x=clarity, fill=color)) + geom_bar() + 
  scale_fill_manual(values=redBlue)

enter image description here

于 2012-12-10T21:32:54.973 回答