r - 试图从 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))
这就是我在 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)))
也尝试手动添加统计信息,这很有效,但仍然没有像原始图表那样在平均值之间画线:
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 版本上的指导?
解决方案
推荐阅读
- javascript - 有什么方法可以在不使用角度材料或引导程序的情况下制作自定义日期选择器?
- swift - 使用面临错误的用户默认值转换 Int 值字符串
- swift - 如果我采用水平收集视图,如何在屏幕中间显示选定的单元格数据
- html - 定义 iframe 大小,然后让它自动调整到浏览器宽度
- javascript - 我有一个 NodeJS 后端,我正在开发与 ReactJS 前端一起工作,但我不断收到 500 错误
- android - mXparser 结果舍入
- python - 从熊猫数据框中删除与另一个数据框中的列组合不匹配的行
- django - 提交时的注册页面导致错误 Django
- python - 有没有一种在离线环境中安装“pip”、“模块”和“依赖项”的有效方法?
- amazon-web-services - aws iot 规则查询语句“union”