【问题标题】:How do you graph a distribution spread over multiple columns?你如何绘制分布在多列上的分布图?
【发布时间】:2020-01-19 14:14:47
【问题描述】:

我收集了包含 1-5 星级餐厅调查评分的汇总数据。

如何绘制分布图并在 R 中找到标准误?

是否可以进行更高级的分析,例如分组评级(例如所有快餐店的错误,即麦当劳+汉堡王、所有牛排馆等)和 t 检验?

数据如下:

Restaurant     Question               1.star  2.stars ...etc

McDonalds      How was the food?      5       6       ...
McDonalds      How were the drinks?   3       4
McDonalds      How were the workers?  2       7
Burger_King    How was the food?      4       11
Burger_King    How were the drinks?   9       3
Burger_King    How were the workers?  12      1

为方便起见,这里是一个 tribble:

tribble(
  ~restaurant, ~question,  ~one_star, ~two_star, ~three_star, ~four_star, ~five_star, ~average,

  "McDonalds", "How was the food?",  5, 6, 8, 2, 9, (5*1 + 6*2 + 8*3 + 2*4 + 5*9)/(5 + 6 + 8 + 2 + 9),
  "McDonalds", "How were the drinks?",  9, 8, 7, 5, 1, (9*1 + 8*2 + 7*3 + 5*4 + 5*1)/(9 + 8 + 7 + 5 + 1),
  "McDonalds", "How were the drinks?",  9, 8, 7, 5, 1, (9*1 + 8*2 + 7*3 + 5*4 + 5*1)/(9 + 8 + 7 + 5 + 1),
  "BurgerKing", "How was the food?",  5, 6, 8, 2, 9, (5*1 + 6*2 + 8*3 + 2*4 + 5*9)/(5 + 6 + 8 + 2 + 9),
  "BurgerKing", "How were the drinks?",  9, 8, 7, 5, 1, (9*1 + 8*2 + 7*3 + 5*4 + 5*1)/(9 + 8 + 7 + 5 + 1),
  "BurgerKing", "How were the drinks?",  9, 8, 7, 5, 1, (9*1 + 8*2 + 7*3 + 5*4 + 5*1)/(9 + 8 + 7 + 5 + 1)
)

【问题讨论】:

    标签: r


    【解决方案1】:

    您的问题范围很广,很难知道您有兴趣从该数据集中提取什么。

    但是,您可以从绘制它们开始,以了解它们所代表的含义。在这里,一个例子来表示从每家餐厅获得的不同星级的问题(我使用tidyr 中的pivot_longer 来重塑数据框,然后使用ggplot2 包来绘制它们):

    library(dplyr)
    library(tidyr)
    library(ggplot2)
    df %>% pivot_longer(., -c(restaurant, question), names_to = "Var", values_to = "Val") %>%
      mutate(Var = factor(Var, levels = c("average","one_star","two_star","three_star","four_star","five_star"))) %>%
      ggplot(aes(x = Var, y = Val, fill = restaurant ))+
      geom_bar(stat = "identity", position = position_dodge())+
      facet_grid(.~question)+
      theme(axis.text.x = element_text(angle = 45, hjust = 1))
    

    由于你的数据基本上是每个餐厅的相同数据,你可以看到对这些数据进行t检验是没有意义的。

    但是在您的真实数据上,比较一些您可以从数据集表示中突出显示的类别到图形中可能是有意义的。

    希望它能帮助您弄清楚如何处理您的数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-03
      • 1970-01-01
      • 2016-11-04
      • 2015-08-06
      • 2020-09-08
      • 1970-01-01
      • 1970-01-01
      • 2021-06-12
      相关资源
      最近更新 更多