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6, 7, 6, 7, 7, 5, 3, 5, 6, 5,
2, 7, 5, 5, 7, 6, 6, 4, 7, 3, 5, 7, 4, 6, 5, 5, 6, 6, 5, 7, 6,
2, 6, 7, 7, 7, 7, 6, 6, 7, 6, 7, 3, 7, 6, 3, 7, 7)), row.names = c(NA,
-60L), class = "data.frame")
我试过了
boxplot(ex)
它给了我一个非常粗略的箱线图。
我的问题是:
获取图形/改进版本的 ggplot 等效项是什么(或者同样的,我只希望 ggplot 代码生成它)。
是否有一个 R 函数可以以图形和表格的形式为我提供关于正态性和异常值的优雅报告(NA 也会很好,但我在发送的样本中没有)
解决方案
问题一:ggplot箱线图
首先将您的数据带入长格式,pivot_longer
然后使用geom_boxplot()
ex_long <- ex %>%
pivot_longer(
cols = everything(),
names_to = "names",
values_to = "values"
)
ggplot(ex_long, aes(x = names, y=values)) +
geom_boxplot()
问题 2a:正常性的视觉和表格检查:
library(tidyverse)
library(ggpubr)
library(rstatix)
library("ggpubr")
# Visual check for normality
# Density plot
ggdensity(ex_long$values, fill = "blue")
# QQ plot
ggqqplot(ex_long$values)
# tabular p values for shapiro test
ex_long %>%
group_by(names) %>%
shapiro_test(values)
问题 2b 异常值:
# set limits of outliers with the percentile method
lower_bound <- quantile(ex_long$values, 0.025)
lower_bound
upper_bound <- quantile(ex_long$values, 0.975)
upper_bound
# Identify all outlieres
outlier_ind <- which(ex_long$values < lower_bound | ex_long$values > upper_bound)
outlier_ind
# print table
ex[outlier_ind, "values"]
# test for outliers with the grubbs test
# install.packages("outliers")
library(outliers)
test <- grubbs.test(ex_long$values)
test
# visualise outliers with `mtext` in ggplot
ggplot(ex_long, aes(x = names, y=values)) +
geom_boxplot()+
mtext(paste("Outliers: ", paste(out, collapse = ", ")))
结论 您的数据不是正态分布的,并且您没有任何异常值!
推荐阅读
- javascript - 在 mobx 商店反应中更新正文样式
- c# - 如何在 DataGridView 的不同行中搜索相同的文本?
- flutter - 如何在颤动中更改光标颜色
- javascript - 如何从组件访问突变数据?
- javascript - 在文本框中只允许前两个和后两个是字母,中间是数字(例如 EE123456789IN)
- c++ - std::string_view 的自定义序列化导致意外的编译器错误
- php - 文件名 http://working.knights.co.in/uploads/retailer/demo.xlsx 在 codeigniter 中不可读
- architecture - 将用户数据存储在身份验证服务器或资源服务器中?或两者?
- java - 如何在另一个创建的类中运行两个线程?
- coq - 逻辑:tr_rev_correct 的辅助引理