【问题标题】:R: Histogram and Density on multiple user response data frameR:多用户响应数据帧上的直方图和密度
【发布时间】:2018-07-03 19:12:13
【问题描述】:

数据说明

数据反映了用户在回答有四个答案的问题时如何对在线图书推荐网站上的图书进行评分。用户可以选择多个答案。

目标是按性别获得分布图,其中X 轴作为答案(X1,X2..)Y 轴作为书籍数量以及密度叠加。男性和女性相互重叠会很棒。

book_id  user_id  rate  X1   X2   X3    X4  Gender  genre
40         001     4.5    0    1    0    0  male    fiction
48         001     3.5    1    0    0    1  male    fiction
54         001     4.0    1    0    0    0  male    fiction
79         001     2.5    1    0    1    0  male    non-fiction
80         001     4.5    0    0    1    0  male    non-fiction
95         001     5.0    1    0    1    0  male    non-fiction
95         002     3.0    0    0    0    1  Female  non-fiction
99         002     4.5    0    0    1    0  Female  non-fiction
02         002     0.5    0    0    0    0  Female  non-fiction
05         002     4.5    1    0    1    0  Female  non-fiction
54         002     4.0    0    1    0    0  Female  fiction
79         002     2.5    1    0    1    0  Female  non-fiction
80         002     4.5    0    0    1    0  Female  non-fiction
07         002     4.5    1    0    1    0  Female  fiction
07         003     5.0    1    0    1    0  Female  fiction
09         003     4.0    0    0    1    0  Female  auto-bio
54         003     4.0    1    0    0    0  Female  fiction
79         003     2.5    1    0    1    0  Female  non-fiction
80         003     4.5    0    0    1    0  Female  non-fction
17         004     3.5    1    0    0    0  male    auto-bio
21         004     5.0    1    0    1    0  male    auto-bio
21         005     5.0    0    1    1    0  male    auto-bio
17         005     0.5    0    0    0    1  male    auto-bio
20         005     5.0    0    0    1    0  male    fiction
20         006     1.5    0    0    0    1  male    fiction
21         006     5.0    0    0    1    0  male    auto-bio
21         007     2.0    1    0    0    0  male    auto-bio
21         008     4.5    1    0    1    0  Female  auto-bio
20         008     4.5    1    0    1    0  Female  fiction
07         008     4.5    1    0    1    0  Female  fiction
22         009     5.0    0    0    1    0  male    fiction
54         009     4.0    1    0    0    0  male    fiction
79         009     2.5    1    0    1    0  male    non-fiction
80         010     4.5    1    0    1    0  male    non-fiction
22         010     4.5    0    1    1    0  male    fiction
22         011     0.5    0    0    1    0  Female  fiction
28         011     3.5    1    0    0    0  Female  auto-bio

两个用户可以对同一本书进行评分,并以相同或不同的方式回答问题。这会为每本书创建两条记录。考虑到这一点,如果按Gender 分组并对每一列求和,则可以从性别水平分布开始。

df %>% group_by(Gender) %>% summarize(x1 = sum(X1), x2 = sum(X2), x3=sum(X3),x4 =sum(X4))

  Gender    x1    x2    x3    x4
  <fct>  <int> <int> <int> <int>
1 Female    10     1    13     1
2 male      10     3    11     3

除了得到剧情之外:我还有以下问题: 也只是为了确认这不是女性答案 x1 的唯一书籍数量,因为同一本书可以由多个用户回答。反而会是多少女性选择一个具体的答案?

【问题讨论】:

  • 你想显示分布的变量是什么?评级?我不清楚X1 等的总和与分布有何关系
  • @camille 我想展示一些具有 X1、X2 答案...并按性别分类的书籍。

标签: r ggplot2 dplyr histogram density-plot


【解决方案1】:

类似但不同的方法

library(data.table)
library(ggplot2)
dt <- setDT(dt)

plottest <- melt(dt,measure.vars = patterns("^X"),variable.name = "question", value.name = "answer")

ggplot(data = plottest,aes(factor(book_id),answer))+
  geom_col(aes(fill = as.factor(question), color = as.factor(question) ))+
  facet_wrap(~Gender)+
  labs(title =  "",
       y = "N",
       x = "books",
       color = "Question",
       fill = "Question")

【讨论】:

  • 包含评分的最佳方式是什么?
  • 如果我使用流派作为 x 轴,而不是书籍。现在每个条形代表一些答案为 x1、x2、x3 的书?也可以转换成百分比吗?
【解决方案2】:

我不确定我是否理解正确,但以下代码是您想要的吗?

library(dplyr)
library(ggplot2)

df2 <- df %>% 
  group_by(Gender) %>% 
  summarize(x1 = sum(X1), x2 = sum(X2), x3=sum(X3),x4 =sum(X4)) %>%
  melt(id.vars = "Gender")


ggplot(df2, aes(variable, value, color = Gender, fill = Gender)) +
  geom_bar(stat = "identity", position = "dodge")

在看到@denis 的答案后,我调整了他的代码,使其大致相同,但使用position = "dodge"

df3 <- df %>% 
  group_by(Gender, book_id) %>% 
  summarize(x1 = sum(X1), x2 = sum(X2), x3=sum(X3),x4 =sum(X4)) %>%
  melt(id.vars = c("Gender", "book_id"))

ggplot(df3, aes(as.factor(book_id), value, color = variable, fill = variable)) +
  geom_bar(stat = "identity", position = "dodge") +
  facet_wrap(~ Gender)

至于第二个问题,您可以使用aggregate获取Gender的每个问题的答案。

aggregate(. ~ Gender, df[4:8], sum)
#  Gender X1 X2 X3 X4
#1 Female 10  1 13  1
#2   male 10  3 11  3

【讨论】:

  • 在那个情节中,你将如何解释 x1 吧,许多女性回答 x1……但没有正确的书籍概念它没有说明。有多少本书女性选择答案1?这就是我想要弄清楚的......
  • 可能是 Introduce book_id in group 条款?
  • 包含rate的最佳方式是什么
  • @Null-Hypothesis 您想将rate 包含在哪个轴上?有另一个图表不是更好吗?
  • 是的,想知道要使用哪些功能。评分 x 1 到 5 以及在 y 轴上每个答案的书籍数量?
猜你喜欢
  • 2012-03-04
  • 1970-01-01
  • 2018-12-16
  • 1970-01-01
  • 2020-08-08
  • 1970-01-01
  • 2013-12-17
  • 1970-01-01
相关资源
最近更新 更多