2014-02-13 41 views
1

想象一下,我有以下矩阵,其中每列对应于一个条。特殊类型的条形图在R

 1 1 3 
    1 3 1 
    1 2 2 

我想做一个堆栈栏图,其中每个数字将确定一个不同颜色的单元块。

例如,如果1 =红2 =蓝3 =绿色,我想得到以下结果:

Sorry about the size of the image. Don't know how to change it.

回答

2
library(ggplot2) 
library(reshape2) 

chartset <- matrix(c(1,1,1,1,3,2,3,1,2), nrow = 3) 
chartsetmelted <- data.frame(melt(chartset)) 

ggplot(chartsetmelted) + geom_tile(aes(x = Var2, y = Var1, fill = factor(value)), width = 0.8) 

你可以看一下scale_fill_manual专门指定颜色值。

+0

正是我在找的东西。谢谢! –