Efficient way to create comparison proportion tables for surveys in R

391 views Asked by At

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.

  1. 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.

  2. 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.

1

There are 1 answers

1
Morris Greenberg On

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:

prop.table(table(df$favorite.color, df$trade.belief))

To visualize this, you could use the facet_wrap() function in ggplot:

df$trade.belief <- as.factor(df$trade.belief)
ggplot(df, aes(x=trade.belief))+geom_bar()+facet_wrap(~favorite.color)

Alternatively, if you wanted them in the same plot, you could just use the fill aesthetic:

ggplot(df, aes(x=trade.belief, fill=favorite.color)) + geom_bar()+scale_fill_manual(values=c('blue', 'green'))