使用分类数据和图表绘制的调查分析
Survey analysis with categorical data and chart plotting
我有一个来自调查的数据库,我从这个数据库用 R 构建了一个数据框,看起来类似于:
cnt <-as.factor(c("Country 1", "Country 2", "Country 3", "Country 1", "Country 2", "Country 3" ))
bnk <-as.factor(c("bank 1", "bank 2", "bank 3", "bank 1", "bank 2", "bank 3" ))
qst <-as.factor(c("q1", "q1", "q1", "q2","q2","q2" ))
ans <-as.numeric(c(1,1,2,1,2,3))
df <-data.frame(cnt, bnk, qst,ans)
names(df) <- c("Country", "Institute", "Question", "Answer")
Country Institute Question Answer
1 Country 1 bank 1 q1 1
2 Country 2 bank 2 q1 1
3 Country 3 bank 3 q1 2
4 Country 1 bank 1 q2 1
5 Country 2 bank 2 q2 2
6 Country 3 bank 3 q2 3
本质上,这个数据框显示有两个不同的问题 - q1、q2,其中参与者 - 这里是来自不同国家的银行 - 必须以一定的数字尺度回答每个问题。
我的目的很简单。我想,对于每个问题,计算并绘制回答 1 的银行的百分比,回答 2 的银行的百分比,等等。
因此,在我们的示例中,有三个银行。关于问题 1,其中 2 人回答 1,1 人回答 2。因此,我想想象一下 - 例如通过条形图 - 有 2/3 的银行(即大约 67%)回答 1 和 1/3(即 aprx. 33% ) 回答了 2. 与问题 2 类似。
不确定,这是否重要,但可能的数字答案范围 可能因问题而异。也就是说,对于 q1,可用答案的范围为 1 到 2,但对于问题 2,可用答案的范围为 1 到 5。
有人可以建议我如何在 R 中快速实现它吗?
当然,一种肮脏的方法是统计bank的个数,统计q1(q2)中"ones"的个数,然后计算各自的分数。然而,这种方法非常耗时,想知道 R 中是否有更好的选择。
更新
完成上述所有操作后,我想回答几个问题来创建一个如下所示的条形图:
在上面的示例中,问题 8 中等于 1 的答案标记为 - "My bank has being ....",等于 2 的答案标记为 "My bank has being started ...",如上图所示。
不过,我们暂时可以忽略"labeling part",因为在x轴上只放1和2就足够了。
这是 ggplot 的快速解答
library(ggplot2)
ggplot(df, aes(x=Question, fill=factor(Answer))) + geom_bar()
输出如下所示:
计算百分比:
library(dplyr)
library(tidyr)
(dat <- df %>% spread(Question, Answer))
Country Institute q1 q2
1 Country 1 bank 1 1 1
2 Country 2 bank 2 1 2
3 Country 3 bank 3 2 3
dat$q1 %>% table/nrow(dat)
1 2
0.6666667 0.3333333
dat$q2 %>% table/nrow(dat)
1 2 3
0.3333333 0.3333333 0.3333333
编辑:为下面的评论添加情节
ggplot(df, aes(x=Answer, fill=factor(Question))) + geom_bar()
编辑:添加以解决更新的问题:
df <- data.frame(answer=c(rep(1, 97), rep(2,3)))
ggplot(df, aes(x=as.factor(answer))) +
geom_bar(aes(y=(..count..)/sum(..count..)), width=.5) +
scale_y_continuous(labels = scales::percent) +
geom_text(aes(y = ((..count..)/sum(..count..)), label = scales::percent((..count..)/sum(..count..))), stat = "count", vjust = -0.25) +
labs(title = "Question 8", y = "Percent", x = "") +
scale_x_discrete(labels=c("My bank has been using \n guarantees already for \n more than 5 years", "My bank has started to use \n guarantees in their last 5 year"))
我有一个来自调查的数据库,我从这个数据库用 R 构建了一个数据框,看起来类似于:
cnt <-as.factor(c("Country 1", "Country 2", "Country 3", "Country 1", "Country 2", "Country 3" ))
bnk <-as.factor(c("bank 1", "bank 2", "bank 3", "bank 1", "bank 2", "bank 3" ))
qst <-as.factor(c("q1", "q1", "q1", "q2","q2","q2" ))
ans <-as.numeric(c(1,1,2,1,2,3))
df <-data.frame(cnt, bnk, qst,ans)
names(df) <- c("Country", "Institute", "Question", "Answer")
Country Institute Question Answer
1 Country 1 bank 1 q1 1
2 Country 2 bank 2 q1 1
3 Country 3 bank 3 q1 2
4 Country 1 bank 1 q2 1
5 Country 2 bank 2 q2 2
6 Country 3 bank 3 q2 3
本质上,这个数据框显示有两个不同的问题 - q1、q2,其中参与者 - 这里是来自不同国家的银行 - 必须以一定的数字尺度回答每个问题。
我的目的很简单。我想,对于每个问题,计算并绘制回答 1 的银行的百分比,回答 2 的银行的百分比,等等。
因此,在我们的示例中,有三个银行。关于问题 1,其中 2 人回答 1,1 人回答 2。因此,我想想象一下 - 例如通过条形图 - 有 2/3 的银行(即大约 67%)回答 1 和 1/3(即 aprx. 33% ) 回答了 2. 与问题 2 类似。
不确定,这是否重要,但可能的数字答案范围 可能因问题而异。也就是说,对于 q1,可用答案的范围为 1 到 2,但对于问题 2,可用答案的范围为 1 到 5。
有人可以建议我如何在 R 中快速实现它吗?
当然,一种肮脏的方法是统计bank的个数,统计q1(q2)中"ones"的个数,然后计算各自的分数。然而,这种方法非常耗时,想知道 R 中是否有更好的选择。
更新
完成上述所有操作后,我想回答几个问题来创建一个如下所示的条形图:
在上面的示例中,问题 8 中等于 1 的答案标记为 - "My bank has being ....",等于 2 的答案标记为 "My bank has being started ...",如上图所示。
不过,我们暂时可以忽略"labeling part",因为在x轴上只放1和2就足够了。
这是 ggplot 的快速解答
library(ggplot2)
ggplot(df, aes(x=Question, fill=factor(Answer))) + geom_bar()
输出如下所示:
计算百分比:
library(dplyr)
library(tidyr)
(dat <- df %>% spread(Question, Answer))
Country Institute q1 q2
1 Country 1 bank 1 1 1
2 Country 2 bank 2 1 2
3 Country 3 bank 3 2 3
dat$q1 %>% table/nrow(dat)
1 2
0.6666667 0.3333333
dat$q2 %>% table/nrow(dat)
1 2 3
0.3333333 0.3333333 0.3333333
编辑:为下面的评论添加情节
ggplot(df, aes(x=Answer, fill=factor(Question))) + geom_bar()
编辑:添加以解决更新的问题:
df <- data.frame(answer=c(rep(1, 97), rep(2,3)))
ggplot(df, aes(x=as.factor(answer))) +
geom_bar(aes(y=(..count..)/sum(..count..)), width=.5) +
scale_y_continuous(labels = scales::percent) +
geom_text(aes(y = ((..count..)/sum(..count..)), label = scales::percent((..count..)/sum(..count..))), stat = "count", vjust = -0.25) +
labs(title = "Question 8", y = "Percent", x = "") +
scale_x_discrete(labels=c("My bank has been using \n guarantees already for \n more than 5 years", "My bank has started to use \n guarantees in their last 5 year"))