R ggplot2堆积条形图,定义条形颜色

时间:2013-05-23 19:56:35

标签: r ggplot2

我非常接近我想要达到的目标,但无法找到一种方法来为我的图表添加画龙点睛。这就是我所拥有的,用foo数据和代码来生成图形 来自重塑包的融化数据:

mdf <- structure(list(v1 = structure(c(1L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 2L, 1L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 2L), .Label = c("ind1", 
"ind10", "ind2", "ind3", "ind4", "ind5", "ind6", "ind7", "ind8", 
"ind9"), class = "factor"), v2 = c(2, 3, 1, 2, 2, 2, 3, 1, 4, 
4, 2, 3, 1, 2, 2, 2, 3, 1, 4, 4), variable = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L), .Label = c("v3", "v4"), class = "factor"), value = c(0.8, 
0.7, 0.7, 0.6, 0.4, 0.3, 0.2, 0.3, 0.9, 0.8, 0.2, 0.3, 0.3, 0.4, 
0.6, 0.7, 0.8, 0.7, 0.1, 0.2)), .Names = c("v1", "v2", "variable", 
"value"), row.names = c(NA, -20L), class = "data.frame")

图表:

library(ggplot2)
ggplot(mdf,aes(x=reorder(v1,v2),y=value,fill=factor(v2))) + geom_bar(stat="identity",color="black")

enter image description here 现在我需要对每个条形图的顶部堆栈进行不同的着色,比如使用一些透明度或事件颜色所有顶部堆栈灰色。我可以使用我的数据集的当前结构来执行此操作,还是应该考虑以不同方式重新整理数据?

由于

编辑:Didzis提供的代码输出图: enter image description here

1 个答案:

答案 0 :(得分:2)

我建议两次使用geom_bar() - 首先,绘制所有数据并设置fill="grey"然后在第二个geom_bar()中使用仅variable为{{1}的数据子集(底栏)和那些使用v3

fill=v2

enter image description here