首页 > 解决方案 > 试图从 ggplot 中重现 ANOVA 测试箱线图

问题描述

目标

我正在尝试用我的 ANOVA 测试重新创建一个 ggplot boxplot,但不知道如何配置它。我在下面提供了我的原始数据:

输出

 structure(list(Year = 2006:2021, Month_USD = c(1160L, 1240L, 
    1360L, 1480L, 1320L, 1320L, 375L, 1600L, 2000L, 2000L, 1600L, 
    2240L, 1900L, 2300L, 2900L, 2300L), Degree = c("High School", 
    "High School", "High School", "High School", "High School", "High School", 
    "High School", "High School", "High School", "BA", "BA", "BA", 
    "BA", "BA", "M.Ed", "M.Ed"), Country = c("USA", "USA", "USA", 
    "USA", "USA", "USA", "DE", "USA", "USA", "USA", "USA", "USA", 
    "PRC", "PRC", "PRC", "HK"), Job = c("Disher", "Prep", "Prep", 
    "Prep", "Prep", "Prep", "Au Pair", "CSA", "Valet", "Valet", "Intake", 
    "CM", "Teacher", "Teacher", "Teacher", "Student"), Median_Household_Income_US = c(4833L, 
    4961L, 4784L, 4750L, 4626L, 4556L, 4547L, 4706L, 4634L, 4873L, 
    5025L, 5218L, 5360L, 5725L, NA, NA), US_Home_Price_Index = c(183.24, 
    173.36, 152.56, 146.69, 140.64, 135.16, 143.88, 159.3, 166.5, 
    175.17, 184.51, 195.99, 204.9, 212.59, 236.31, NA)), class = "data.frame", row.names = c(NA, 
    -16L))

当我运行我原来的方差分析并在 ggplot 中创建我的箱线图时,我想出了这个:

ANOVA 和 GGPlot 箱线图

                          #         ANOVA       #

Degree_AOV <- Earnings_Year %>% 
  anova_test(Month_USD~Degree)
Degree_AOV
# 63% of the effect size is due to degree


#Post-Hoc Tests

pwc <- Earnings_Year %>% 
  tukey_hsd(Month_USD~Degree)
pwc
#difference between HS/BA sig, BA/MED not sig

                          #   GGPlot Boxplot #

pwc <- pwc %>% 
  add_xy_position(x="Degree")

ggboxplot(Earnings_Year,
          x="Degree",
          y="Month_USD", fill="Degree")+
  stat_pvalue_manual(pwc,hide.ns = TRUE)+
  labs(title="One-Way ANOVA: Degree Impact on Salary",
       subtitle = get_test_label(Degree_AOV,
                                 detailed = TRUE),
       caption = get_pwc_label(pwc))+
  theme(plot.title = element_text(face="bold",
                                  size=20))

GGPLOT

这就是我在 Plotly 中所能想到的,因为我不知道如何添加 p 值等:

情节

plot_ly(data = Earnings_Year,
        x=~Degree,
        y=~Month_USD,
        type = "box",
        color = ~Degree) %>% 
  layout(title="ANOVA Boxplot",
         xaxis= list(title="Degree"),
         yaxis= list(title="Monthly USD")) %>% 
  config(displayModeBar=FALSE) 

阴谋

也尝试了ggplotly,但似乎与显示的标签不太相符:

plotly::ggplotly(ggboxplot(Earnings_Year,
                           x="Degree",
                           y="Month_USD", fill="Degree")+
                   stat_pvalue_manual(pwc,hide.ns = TRUE)+
                   labs(title="One-Way ANOVA: Degree Impact on Salary",
                        subtitle = get_test_label(Degree_AOV,
                                                  detailed = TRUE),
                        caption = get_pwc_label(pwc))+
                   theme(plot.title = element_text(face="bold",
                                                   size=20)))

阴谋 GGPLOTLY

也尝试手动添加统计信息,这很有效,但仍然没有像原始图表那样在平均值之间画线:

plotly::ggplotly(ggboxplot(Earnings_Year,
                           x="Degree",
                           y="Month_USD", fill="Degree")+
                   stat_pvalue_manual(pwc,hide.ns = TRUE)+
                   labs(title="One-Way ANOVA: Degree Impact on Salary",
                        subtitle = get_test_label(Degree_AOV,
                                                  detailed = TRUE),
                        caption = get_pwc_label(pwc))+
                   theme(plot.title = element_text(face="bold",
                                                   size=20))) %>% 
  layout(annotations = 
           list(x = 0.4, y = 0.9, 
                text = "ANOVA, F(2,13)=11.14,p=0.002", 
                showarrow = F, xref='paper', yref='paper', 
                xanchor='right', yanchor='auto', xshift=0, yshift=0,
                font=list(size=10, color="black")))

标签情节

谁能提供有关如何使 ANOVA 值显示在 Plotly 版本上的指导?

标签: rggplot2plotly

解决方案


推荐阅读