I am using ggplot to plot Proportional Stacked Bar plot. And the Plot I am getting is something like this:
Thanks @zelite and @SimonO101 for your help. This is simpler version of what both of you proposed. Adding here for the completeness.
library(ggplot2)
library(reshape2)
library(RColorBrewer)
getColors<-function(n){
mypal<-colorRampPalette(brewer.pal(12, "Paired"))
sample(mypal(n), n, replace=FALSE)
}
PropBarPlot<-function(df, mytitle=""){
melteddf<-melt(df, id=names(df)[1], na.rm=T)
n<-length(levels(factor(melteddf$variable)))
ggplot(melteddf, aes_string(x=names(df)[1], y="value", fill="variable")) +
geom_bar(position="fill") +
scale_fill_manual(values=getColors(n)) +
theme(axis.text.x = element_text(angle=90, vjust=1)) +
labs(title=mytitle)
}
df <- data.frame(id=letters[1:3],
val0=1:3,
val1=4:6,
val2=7:9,
val3=2:4,
val4=1:3,
val5=4:6,
val6=10:12,
val7=12:14)
print(PropBarPlot(df))
Thanks.