I'd like a way to simply and efficiently create proportion comparison tables for comparing survey answers of populations and subsets of populations.
Here's a sample dataset:
id <- c(11, 12, 13, 14, 15, 16)
gender <- c("M", "M", "F", "F", "M", "F" )
trade.belief <- c("I love NAFTA", "I hate NAFTA", "I love NAFTA", "I hate NAFTA", "I hate NAFTA", "I love NAFTA" )
favorite.color <-c("My favorite color is green", "My favorite color is green", "My favorite color is blue", "My favorite color is blue", "My favorite color is blue", "My favorite color is green")
votes.in.elections <- c("Yes", "Yes", "No", "Yes", "Yes", "Yes")
df <- data.frame(id, gender, trade.belief, votes.in.elections, favorite.color)
df.green <- df[df$favorite.color == "My favorite color is green",]
df.blue <- df[df$favorite.color == "My favorite color is blue",]
Most online survey tools would have an efficient way to filter or select people who like the color green and compare, side by side, to the the general population or people who like blue, for all survey questions.
So, if I wanted to see how most people vote or like NAFTA is their favorite color is green, I could do
table(df[df$favorite.color == "My favorite color is green",])
But, this isn't good for comparing to other subsetted populations, and it's also a very inefficient way to look at the data.
I'd like a way to visualize in tables people who like green, to both the general population and people who like blue, on all survey questions and variables. Proportional tables, not counts.
Ideally, I'd also like a way to visualize this with bar charts. My thought is that one would want to use the data.wrapper feature on ggplot.
For getting a nice table comparing different favorite colors and different levels of love for NAFTA by percentages, you can use the
prop.table()
function:To visualize this, you could use the
facet_wrap()
function in ggplot:Alternatively, if you wanted them in the same plot, you could just use the fill aesthetic: