Luis
Luis

Reputation: 1584

ggplot for multiple categorical variables -- count data

as always, this forum is my last hope to find a solution to my question. I'm working on a dataset where some participants (children) received an intervention program to improve their social skills/attitudes. Before the treatment, all participants saw a video clip where a "soccer game", a "basketball" and "snooker" happened and the actors were "aggressive", or "assertive" or "neutral".

All participants replied if the actor behavior was "wrong", "right" or "I don't know". After the intervention, they saw the same videos and they had to say if the action was "wrong", "right" or "I don't know".

We expect that -- after the intervention program, the participants will say "right" for the "assertive" behavior and "wrong" for the "aggressive" behavior for all situations.

I'm dealing with this question using Mcnemar test approach, but I'm having some difficulties to plot a nice graph and I really think this forum community will help me to understand what's going on.

The following code works and is a reproducible example and reply to the question for each situation -- what attitude increased/decreased after the invervention.

library(tidyverse)
set.seed(123)
ds <- data.frame(ID=seq.int(nrow(ds)),
                 situation=rep(c("Soccer","Basketball","snooker"),20),
                 attitude=c("aggressive","assertive","neutral"),
                 response_t1=c("wrong","Right","I Don't"),
                 response_t2=rep(c("wrong","Right","I Don't"), times=c(10,35,15)))

ds %>% 
  gather(key="Time",value, response_t1:response_t2) -> j

j  %>% 
  group_by(attitude, Time, situation, value) %>% 
  summarise(n = n()) %>% 
  ggplot(., aes(x = value, y = n, fill=Time)) + 
  geom_bar(stat = "identity", width=0.5, position = position_dodge(width=0.6)) +
  facet_wrap(~ situation*attitude, ncol = 3) 

Valid XHTML.

As you can see, the plot is "almost ok", however, the bar width is different across bars.

Two questions: Do you suggest another graph approach? Do you suggest another statistical approach?

This graph here is very interesting https://i.sstatic.net/EIQZd.jpg

Upvotes: 2

Views: 2632

Answers (1)

Tung
Tung

Reputation: 28371

You can use option preserve = "single" to get the same bar width everywhere

library(tidyverse)
set.seed(123)
ds <- data.frame(
  ID = seq.int(60),
  situation = rep(c("Soccer", "Basketball", "snooker"), 20),
  attitude = c("aggressive", "assertive", "neutral"),
  response_t1 = c("wrong", "Right", "I Don't"),
  response_t2 = rep(c("wrong", "Right", "I Don't"), times = c(10, 35, 15))
)

ds %>%
  gather(key = "Time", value, response_t1:response_t2) -> j
#> Warning: attributes are not identical across measure variables;
#> they will be dropped

j %>%
  group_by(attitude, Time, situation, value) %>%
  summarise(n = n()) %>%
  ggplot(., aes(x = value, y = n, fill = Time)) +
  geom_col(width = 0.5, position = position_dodge(preserve = "single", width = 0.6)) +
  facet_wrap(~situation * attitude, ncol = 3)

Created on 2018-08-07 by the reprex package (v0.2.0.9000).

Upvotes: 3

Related Questions