ggplot 条形图到箱线图
ggplot barplot to boxplot
我有生成好的条形图的代码,我正在尝试用相同的数据创建一个箱线图。
条形图显示所有人 (id) 的“响应”计数。我想为每种类型的“响应”创建一个箱线图来替换 3 个条形图。箱线图应该根据每个参与者的特定“响应”的计数来计算。到目前为止运气不好,因为我一直在研究如何计算每个参与者的响应。
当前代码:
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))
输出:
数据样本:
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"))
你可以试试:
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")
请注意,箱线图不适用于离散数据,例如小计数(除非您的实际数据有更多的参与者,每个响应的计数也更高)
您可以使用此代码:
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")
输出:
我有生成好的条形图的代码,我正在尝试用相同的数据创建一个箱线图。 条形图显示所有人 (id) 的“响应”计数。我想为每种类型的“响应”创建一个箱线图来替换 3 个条形图。箱线图应该根据每个参与者的特定“响应”的计数来计算。到目前为止运气不好,因为我一直在研究如何计算每个参与者的响应。
当前代码:
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))
输出:
数据样本:
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"))
你可以试试:
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")
请注意,箱线图不适用于离散数据,例如小计数(除非您的实际数据有更多的参与者,每个响应的计数也更高)
您可以使用此代码:
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")
输出: