ggplot用于多个类别变量-计数数据 [英] ggplot for multiple categorical variables -- count data

查看:56
本文介绍了ggplot用于多个类别变量-计数数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与往常一样,这个论坛是我最后一个希望找到解决我问题的方法.我正在研究一个数据集,其中一些参与者(儿童)接受了一项干预计划,以提高他们的社交技能/态度.在治疗之前,所有参与者都观看了一段视频片段,其中发生了足球比赛",篮球"和斯诺克",并且演员是好斗的",自信的"或中立的".

所有参与者回答演员的行为是错误",正确"还是我不知道".干预后,他们看到了相同的视频,不得不说动作是错误",正确"还是我不知道".

我们希望-在干预计划结束后,参与者将对所有情况的肯定"行为说对",对所有情况的攻击性"行为说对".

我正在使用麦克纳马克测验方法来处理这个问题,但是我在绘制漂亮的图形时遇到了一些困难,我真的认为这个论坛社区将帮助我了解正在发生的事情.

以下代码有效,并且是可复制的示例,并且在每种情况下均会回答该问题-发明后哪种态度会增加/减少.

 库(tidyverse)set.seed(123)ds<-data.frame(ID = seq.int(nrow(ds)),情境= rep(c(足球",篮球",斯诺克"),20),态度= c(积极",自信",中立"),response_t1 = c(错误",正确",我不"),response_t2 = rep(c("wrong","Right","I Do n't"),times = c(10,35,15)))ds%>%collect(key ="Time",value,response_t1:response_t2)->Ĵj%&%;%group_by(态度,时间,状况,价值)%&%;%summarise(n = n())%>%ggplot(.,aes(x =值,y = n,填充=时间))+geom_bar(stat ="identity",width = 0.5,position = position_dodge(width = 0.6))+facet_wrap(〜情境*态度,ncol = 3) 

.

如您所见,该图几乎是可以的,但是,条的宽度在条之间是不同的..

两个问题:您是否建议另一种图形方法?您是否建议另一种统计方法?

此图(v0.2.0.9000)创建.

.>

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) 

.

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://imgur.com/a/lyQB11E

解决方案

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

这篇关于ggplot用于多个类别变量-计数数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆