compare different datasets with stacked bar graphs in R -
i need compare 2 different methods each of them has 3 different results in 1 graph using stacked bar style.
i want draw plot x axis shows experiment , y axis shows results. , each bar fills 3 results in stacked bar format.
experiment method resuult1 result2 result3 1 m1 1 2 3 1 m2 4 5 6 2 m1 7 8 9 2 m2 10 11 12 3 m1 13 14 15 3 m2 16 17 18
i have code comparing 2 info set how can alter it.
library(ggplot2); pdf(file = '$filename.pdf', width=5, height=5); data1 <- as.matrix(read.table('$input_file1', header = t)); data1.experiment <- as.numeric(data1[,\"experiment\"]); data1.obs <- as.numeric(data1[,\"result1\"]); data1.method <- as.factor(data1[,\"method\"]); df <- data.frame(data1.experiment, data1.method, data1.obs);
orderlist = c("70", "100", "130", "160", "190", "260");
ggplot(df, aes(x = data1.experiment, y = data1.obs, fill = data1.method), ylim=c(60000, 2800000)) + geom_bar(stat='identity', position='dodge')+ labs(x='$xlabel',y='$ylabel', fill='methods') + scale_fill_manual(values = c('red','blue'), labels = c('dtb-mac', 'ieee802.11p')) + scale_x_continuous(breaks = orderlist)+ theme(legend.position = c(1, 1), legend.justification = c(1, 1), legend.background = element_rect(colour = na, fill = 'white'));
you said need compare methods. if represent experiment on x-axis , result on y how represent method??? way of doing using facet. here code how using ggplot2.
dat <- read.csv("data.csv") library(reshape2) library(ggplot2) dat1 <- melt(dat,id.vars = c("experiment","method")) p <- ggplot(dat1,aes(experiment,value,fill=variable))+geom_bar(stat="identity")+ facet_wrap(~method,nrow=1) p
r