plot - How to preprocess data to generate barplots in R -
i want generate (grouped , stacked) bar plots values have in lists:
dog = list(a=100, c=30, t=140, g=102) cat = list(a=99, c=31, t=150, g=123) pig = list(a=100, c=12, t=90, g=144) in first barplot, data should groupe letter (actg) , each animal should have it's own bar.
the second barplot should stacked plot that shows each animal percentage of a, c, t , g.
in help(barplot) read, need generate matrix-like datastructure. preferred way put data matrix?
in example saw, people use ?tables have names in it. difference between table matrix, , how can generate table data?
you can create matrix with:
data <- matrix(c(dog, cat, pig), nrow=3, ncol=4, dimnames=list(c("dog", "cat", "pig"), c("a", "c", "t", "g"))) data > dog cat pig > 100 99 100 > c 30 31 12 > t 140 150 90 > g 102 123 144 plot:
barplot(data) and result:

x <- data.frame( animals=c(rep("dog",4),rep("cat",4),rep("pig",4)), gen=c(rep(c("a","c","t","g"),3)), value=c(100,30,140,102,99,31,150,123,100,12,90,144)) > animals gen value > 1 dog 100 > 2 dog c 30 > 3 dog t 140 > 4 dog g 102 > 5 cat 99 > 6 cat c 31 > 7 cat t 150 > 8 cat g 123 > 9 pig 100 > 10 pig c 12 > 11 pig t 90 > 12 pig g 144 with of:
library(ggplot2) library(reshape2) ggplot(mx, aes(x=animals,y=value)) + geom_bar(stat="identity") + facet_grid(~gen) result:

Comments
Post a Comment