首页
学习
活动
专区
圈层
工具
发布

到boxplot
EN

Stack Overflow用户
提问于 2022-03-07 11:59:35
回答 2查看 73关注 0票数 0

我有代码来生成好的barplot,并且我尝试用相同的数据创建一个盒图。条形图显示所有人员(id)的“响应”计数。我想为每种类型的“响应”创建一个方格图来替换这3条。应从每个参与者的具体“响应”计数中计算出“方框图”。到目前为止,没有运气,因为我被困在如何计算每个参与者的反应。

当前代码:

代码语言:javascript
复制
df %>%
  ggplot(position = dodge) + 
  labs(title= "question") +
  geom_bar(aes(x = response), fill="red") +
  labs(y = "count", x = "responses") +
  scale_y_continuous(breaks=seq(0,100,20), limits = c(0,100)) 

产出:

数据样本:

代码语言:javascript
复制
structure(list(id = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5), 
    response = c(0, 1, 1, 0, 0, 0, 1, -1, 1, -1, 0, 1, -1, 1, 
    0, 0, 0, 0, 1, 1, 1, -1, 0, 1, 0, 1, 1, -1, 0, 1, 1, 1, 0, 
    1, 0, 0, 1, -1, 0, 1, 1, 1, -1, 1, 1, 1, 0, 0, -1, 1, 1, 
    -1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 
    1, 1, 0, 0, 0), iscorrect = c(0, 1, 1, 0, 0, 0, 1, 0, 1, 
    0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 
    0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 
    0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 
    0, 1, 0, 0, 1, 1, 0, 0, 0), min = c(100, 150, 150, 
    50, 50, 50, 150, 100, 100, 100, 50, 100, 50, 150, 150, 150, 
    50, 100, 100, 100, 150, 150, 50, 50, 50, 150, 150, 100, 50, 
    100, 100, 150, 150, 50, 50, 50, 150, 100, 100, 100, 50, 100, 
    50, 150, 150, 150, 50, 100, 100, 100, 150, 150, 50, 50, 50, 
    150, 150, 100, 50, 100, 100, 150, 100, 50, 50, 50, 150, 100, 
    100, 50, 150, 100, 50, 150, 150), max = c(125.4, 180.8, 
    180.8, 62.4, 62.4, 62.4, 180.8, 125.4, 125.4, 125.4, 62.4, 
    125.4, 62.4, 180.8, 180.8, 180.8, 62.4, 125.4, 125.4, 125.4, 
    180.8, 180.8, 62.4, 62.4, 62.4, 180.8, 180.8, 125.4, 62.4, 
    125.4, 125.4, 180.8, 180.8, 62.4, 62.4, 62.4, 180.8, 125.4, 
    125.4, 125.4, 62.4, 125.4, 62.4, 180.8, 180.8, 180.8, 62.4, 
    125.4, 125.4, 125.4, 180.8, 180.8, 62.4, 62.4, 62.4, 180.8, 
    180.8, 125.4, 62.4, 125.4, 125.4, 180.8, 125.4, 62.4, 62.4, 
    62.4, 180.8, 125.4, 125.4, 62.4, 180.8, 125.4, 62.4, 180.8, 
    180.8), time = c(5, 7, 9, 5, 1, 7, 1, 1, 7, 3, 9, 9, 
    3, 5, 3, 1, 9, 5, 1, 7, 9, 3, 5, 7, 1, 5, 7, 3, 3, 9, 5, 
    7, 9, 5, 1, 7, 1, 1, 7, 3, 9, 9, 3, 5, 3, 1, 9, 5, 1, 7, 
    9, 3, 5, 7, 1, 5, 7, 3, 3, 9, 9, 7, 5, 7, 5, 9, 5, 3, 1, 
    1, 9, 7, 3, 3, 1)), row.names = c(NA, -75L), class = c("tbl_df", 
"tbl", "data.frame"))
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-03-07 15:22:59

您可以使用以下代码:

代码语言:javascript
复制
data %>% 
  group_by(id) %>%
  count(response) %>%
  mutate(response = as.factor(response)) %>%
  ggplot(aes(x = response, y = n)) +
  geom_boxplot(fill = "red") +
  labs(y = "count", x = "responses")

输出:

票数 1
EN

Stack Overflow用户

发布于 2022-03-07 12:47:54

你可以试试:

代码语言:javascript
复制
library(dplyr)
library(ggplot2)

df %>% 
  group_by(id, response) %>% 
  count() %>%
  mutate(id = factor(id), response = factor(response)) %>%
  ggplot(aes(response, n)) + 
  geom_boxplot(fill = "red") +
  scale_y_continuous(name = "Number of responses per participant")

请注意,对于小计数之类的离散数据,方格图不能很好地工作(除非实际数据有更多的参与者,每个响应的计数要高得多)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71380750

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档