首页 > 解决方案 > 在 R 中创建具有多个组的箱线图

问题描述

我使用 ggplot 为我拥有的一组数据制作了一个包含多个组的条形图(使用 7 点李克特量表测量脉冲宽度和频率的紧迫性、烦恼、效价和唤醒水平)。分组基于我的数据集中的变量,这是我需要的

在此处输入图像描述

我使用的代码如下:

breaks = c(1,2,3,4,5,6,7)
labels = as.character(breaks)
breaks2 = c(10, 30, 50, 70, 90, 110)
labels2 = as.character(breaks2)
BarChart_Freq_Dvs = ggplot(AllResults, aes(Frequency, value, group = variable,
                                           shape = variable, linetype=variable, fill = variable))
BarChart_Freq_Dvs + 
stat_summary(fun.y = mean, geom = "bar", width = 9,  position = position_dodge(width = 11))+
stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width = 7, position = position_dodge(width 
= 11))+
scale_y_continuous(name = "likert scale",
                 limits = c(1,7), breaks = breaks, labels = labels, oob = rescale_none)+
scale_x_continuous(name = "Frequency",
                 limits = c(10,110),   breaks = breaks2, labels = labels2, oob = rescale_none, expand 
= c(0.06,0))+
theme_classic()+
theme(panel.background = element_rect(fill = NA),
    panel.grid.major.y = element_line(colour = "grey50"),
    panel.ontop = TRUE)

然后我决定用箱形图替换条形图并写道:

Box_Plot = ggplot(AllResults, aes(Frequency, value, group = variable,
                                         shape = variable, linetype=variable, fill = variable))
Box_Plot +
geom_boxplot() + 
scale_y_continuous(name = "likert scale", limits = c(1,7), breaks = breaks, labels = labels, oob = 
rescale_none)+
scale_x_continuous(name = "Frequency",
                   limits = c(10,110),   breaks = breaks2, labels = labels2, oob = rescale_none, 
expand = c(0.06,0))+
theme_classic()+
theme(panel.background = element_rect(fill = NA),
      panel.grid.major.y = element_line(colour = "grey50"),
      panel.ontop = TRUE)

但我得到了下面的图表,它确实有意义

箱形图

我在互联网上搜索了一个修复程序,我发现的所有代码都和我写的几乎一样。我不知道我的代码问题出在哪里。

样本 :

PW     Frequency        variable      value 
High     110            urgency        3
Middle    10            urgency        2
Low       70            urgency        2
High      30            urgency        5
Middle    50            urgency        3
Low       70            annoyance      5
Middle    50            annoyance      4
High      90            annoyance      7
Middle    90            annoyance      5
High      110           annoyance      7
High      10            annoyance      2
High      50            valence        3
Middle    10            valence        4
Low       50            valence        4
Middle    50            valence        4
Low       30            valence        4
Middle    30            valence        4
High      90            arousal        4
Middle    110           arousal        3
High      70            arousal        3
Middle    70            arousal        2
Middle    90            arousal        2
Low       10            arousal        1

标签: rggplot2staticbar-chartboxplot

解决方案


推荐阅读