1

Goal

I'm trying to recreate a ggplot boxplot with my ANOVA test in plotly, but not sure how to configure it. I have provided my raw data below:

Dput

 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))

When I ran my original ANOVA and created my boxplot in ggplot, I came up with this:

ANOVA and GGPlot Boxplot

                          #         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

This is all I can come up with in Plotly because I have no idea how to add the p values and such:

Plotly

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) 

PLOTLY

Tried ggplotly as well and doesn't seem to quite work with the labels showing up:

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)))

PLOTLY GGPLOTLY

Tried manually adding the stats too, which worked, but still don't have the lines draw between means like the original graph:

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")))

LABEL PLOTLY

Can anyone offer guidance on how to make the ANOVA values show up on the Plotly version?

Shawn Hemelstrand
  • 2,676
  • 4
  • 17
  • 30

0 Answers0