Reputation: 475
I have a simple data frame that I made by binding two columns from another larger table (with cbind). Both my columns have numeric values. Column 1 (education) has 5 possible values (1=no school, 2=high school, 3=undergrad, 4=masters, 5=PhD). Column 2 (answer) has 4 possible answers. I want to make a plot that shows me the instances of the answers, that somehow visualizes that for instance there were 500 people who had level 2 education and answered 2, whereas 100 people who had level 3 education answered 2, and only 10 people with level 4 education answered 2. And then 1000 who had level 3 education answered 5, and 100 who had level 4 education answered 5 etc etc. When I use "table" the numbers come out right:
1 2 3 4 5
1 75 142 206 71 41
2 179 432 570 250 139
3 177 503 669 417 254
4 113 235 342 292 227
But no plot I've tried shows anything useful. I've tried plot, boxplot, hist, mosaicplot... do I need to use another plot? Is there something else going on?
Upvotes: 0
Views: 179
Reputation: 3604
For fun, a base graphics alternative:
m = matrix(c(75, 142, 206, 71, 41,
179, 432, 570, 250, 139,
177, 503, 669, 417, 254,
113, 235, 342, 292, 227), nrow=4, byrow=T)
barplot(t(m), names.arg=1:4, legend.text=c("no school", "high school", "undergrad","masters","PhD"), args.legend=list(x='top', inset=c(0,-0.3), ncol=2, cex=0.6))
Upvotes: 2
Reputation: 92282
Is this what you are looking for?
library(reshape2)
library(ggplot2)
df <- read.table(text=("1 2 3 4 5
1 75 142 206 71 41
2 179 432 570 250 139
3 177 503 669 417 254
4 113 235 342 292 227"), header = T)
df$Answer <- 1:4
df <- melt(df, "Answer")
df$variable <- factor(df$variable, labels = c("no school", "high school", "undergrad","masters","PhD"))
names(df)[2] <- "Education"
ggplot(df, aes(x = Answer, y = value)) +
geom_bar(stat = "identity", aes(fill = Education)) +
ylab("Answer frequencies")
Upvotes: 0