在 ggplot boxplot 中绘制多个框?
Plotting more than one box in ggplot boxplot?
抱歉没有使用正确的 "lingo",我不知道它叫什么:
我有一个 table 由表示参与者对问题的数字回答的数据组成。数据 table 类似于下面的示例(为清楚起见明显缩短):
participant q1 q2 q3 q4 .... q10
1 2 1 3 5 .... 2
2 3 2 4 1 .... 4
3 1 2 4 2 .... 3
.
.
50 2 3 5 2 .... 5
所以我想做的是在 ggplot 中创建一个箱线图,将问题编号沿 x 轴放置,并在一边得分。我知道如何只用一个问题来绘制箱线图,但我该如何绘制所有十个问题?
如果我这样做:
susQBoxPlot <-ggplot(susQuestions, aes(x = participant, y = q1, group = 1))
+ geom_boxplot()
susQBoxPlot
然后我明白了:
但是我该从这里去哪里呢?我以为我可以将额外的列添加到 aes 的 "y =" 部分,如下所示:
susQBoxPlot <-ggplot(susQuestions, aes(x = participant, y = q1, q2, group = 1))
+ geom_boxplot()
但它只是给了我相同的输出。
接下来我尝试了这个:
susQBoxPlot <-ggplot(susQuestions, aes(x = participant, y = c(q1, q2), group = 1))
+ geom_boxplot()
susQBoxPlot
但是我得到以下错误:
Error: Aesthetics must be either length 1 or the same as the data (50): y
随便什么意思!
我试过查看 ggplot 文档,但我看不到任何看起来像我正在尝试做的事情。
是的,我知道 r 有一个内置的 boxplot() 函数,但我不想使用它,因为我希望我的箱形图和条形图具有相同的样式,但我不想不喜欢 r 中 barplot() 函数的工作方式!
你会想做类似
ggplot(tidyr::gather(susQuestions, q, val, -participant), aes(q, val, group=q)) + geom_boxplot()
好的,我成功了。感谢 Robin Gertenbach 建议我执行以下操作:
ggplot(tidyr::gather(susQuestions, q, val, -participant), aes(q, val, group=q)) + geom_boxplot()
这创建了一个箱线图,给出了我想要的结果,但 x 轴值出现了乱序(即它们变为 q1, q10, q2, q3, q4....
)。我找到了这个 here, and used Tjebo's 解决方案的解决方案。
最后我的代码是这样的:
# Re-organise susQuestions data frame into long format:
questionData <- tidyr::gather(susQuestions, q, val, -participant)
# Create a vector of question levels
questionLevels <- c("q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8", "q9", "q10")
# Create box plot
susQBoxPlot <-ggplot(questionData, aes(x = factor(q, questionLevels), val, group=q)) + # Define data for plot
stat_boxplot(geom ='errorbar', width=0.25) + # Add error bars
geom_boxplot(fill = "red", colour = "black") + # Set fill colour to blue
scale_y_continuous(name = "SUS Score", breaks = seq(1, 5, 1), limits=c(1, 5)) + # Set scale for y axis
scale_x_discrete(name = "Question") + # Set x axis name
ggtitle("Boxplot of SUS Question Responses") + # Set plot title
theme_bw() + # Set black and white theme
theme(plot.title = element_text(hjust = 0.5), # Centre plot title
panel.grid.major = element_blank(), # Turn off major gridlines
panel.grid.minor = element_blank()) # Turn off minor gridlines
susQBoxPlot # Display plot
结果:
抱歉没有使用正确的 "lingo",我不知道它叫什么:
我有一个 table 由表示参与者对问题的数字回答的数据组成。数据 table 类似于下面的示例(为清楚起见明显缩短):
participant q1 q2 q3 q4 .... q10
1 2 1 3 5 .... 2
2 3 2 4 1 .... 4
3 1 2 4 2 .... 3
.
.
50 2 3 5 2 .... 5
所以我想做的是在 ggplot 中创建一个箱线图,将问题编号沿 x 轴放置,并在一边得分。我知道如何只用一个问题来绘制箱线图,但我该如何绘制所有十个问题?
如果我这样做:
susQBoxPlot <-ggplot(susQuestions, aes(x = participant, y = q1, group = 1))
+ geom_boxplot()
susQBoxPlot
然后我明白了:
但是我该从这里去哪里呢?我以为我可以将额外的列添加到 aes 的 "y =" 部分,如下所示:
susQBoxPlot <-ggplot(susQuestions, aes(x = participant, y = q1, q2, group = 1))
+ geom_boxplot()
但它只是给了我相同的输出。
接下来我尝试了这个:
susQBoxPlot <-ggplot(susQuestions, aes(x = participant, y = c(q1, q2), group = 1))
+ geom_boxplot()
susQBoxPlot
但是我得到以下错误:
Error: Aesthetics must be either length 1 or the same as the data (50): y
随便什么意思!
我试过查看 ggplot 文档,但我看不到任何看起来像我正在尝试做的事情。
是的,我知道 r 有一个内置的 boxplot() 函数,但我不想使用它,因为我希望我的箱形图和条形图具有相同的样式,但我不想不喜欢 r 中 barplot() 函数的工作方式!
你会想做类似
ggplot(tidyr::gather(susQuestions, q, val, -participant), aes(q, val, group=q)) + geom_boxplot()
好的,我成功了。感谢 Robin Gertenbach 建议我执行以下操作:
ggplot(tidyr::gather(susQuestions, q, val, -participant), aes(q, val, group=q)) + geom_boxplot()
这创建了一个箱线图,给出了我想要的结果,但 x 轴值出现了乱序(即它们变为 q1, q10, q2, q3, q4....
)。我找到了这个 here, and used Tjebo's 解决方案的解决方案。
最后我的代码是这样的:
# Re-organise susQuestions data frame into long format:
questionData <- tidyr::gather(susQuestions, q, val, -participant)
# Create a vector of question levels
questionLevels <- c("q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8", "q9", "q10")
# Create box plot
susQBoxPlot <-ggplot(questionData, aes(x = factor(q, questionLevels), val, group=q)) + # Define data for plot
stat_boxplot(geom ='errorbar', width=0.25) + # Add error bars
geom_boxplot(fill = "red", colour = "black") + # Set fill colour to blue
scale_y_continuous(name = "SUS Score", breaks = seq(1, 5, 1), limits=c(1, 5)) + # Set scale for y axis
scale_x_discrete(name = "Question") + # Set x axis name
ggtitle("Boxplot of SUS Question Responses") + # Set plot title
theme_bw() + # Set black and white theme
theme(plot.title = element_text(hjust = 0.5), # Centre plot title
panel.grid.major = element_blank(), # Turn off major gridlines
panel.grid.minor = element_blank()) # Turn off minor gridlines
susQBoxPlot # Display plot
结果: