Say I have two factors and I want to graph them on the same plot, both factors have the same levels.
s1 <- c(rep("male",20), rep("female", 30))
s2 <- c(rep("male",10), rep("female", 40))
s1 <- factor(s1, levels=c("male", "female"))
s2 <- factor(s2, levels=c("male", "female"))
I would have thought that using the table function would have produced the correct result for graphing but it pops out.
table(s1, s2)
s2
s1 male female
male 10 10
female 0 30
So really two questions, what is the table function doing to get this result and what other function can i use to create a graph with 2 series using functions with the same levels?
Also if it is a factor I'm using barplot2 in the gplots package to graph it.
You can achieve slightly more detailed results with lattice package:
s1 <- factor(c(rep("male",20), rep("female", 30)))
s2 <- factor(c(rep("male",10), rep("female", 40)))
D <- data.frame(s1, s2)
library(lattice)
histogram(~s1+s2, D, col = c("pink", "lightblue"))
Or if you want males/females side by side for easier comparison:
t1 <- table(s1)
t2 <- table(s2)
barchart(cbind(t1, t2), stack = F, horizontal = F)