Reputation: 1572
I'm very close to what I want to achieve but can't find a way to add the finishing touch to my graph. Here's what I have, with foo data and code to produce the graph.
Melted data from the reshape package:
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")
Graph :
library(ggplot2)
ggplot(mdf,aes(x=reorder(v1,v2),y=value,fill=factor(v2))) + geom_bar(stat="identity",color="black")
Now I need to color the top stack of each bar differently, say with some transparency or event color all the top stack grey. Can I do that with the current structure of my data set or should I consider reshaping my data differently ?
Thanks
EDIT : output graph from the code provided by Didzis :
Upvotes: 1
Views: 2866
Reputation: 98419
I would suggest to use geom_bar()
twice - first, plot all data and set fill="grey"
then in second geom_bar()
use only subset of data where variable
is v3
(bottom bars) and for those use fill=v2
.
ggplot(mdf,aes(x=reorder(v1,v2),y=value)) +
geom_bar(stat="identity",color="black",fill="grey")+
geom_bar(data=subset(mdf,variable=="v3"),
aes(x=reorder(v1,v2),y=value,fill=factor(v2)),
stat="identity",color="black")
Upvotes: 2