# 安装与载入R包
install.packages("rempsyc")
library(rempsyc)
R包中函数nice_scatter(),画出散点图:nice_scatter(
data = iris,
predictor = "Sepal.Length", # 自变量
response = "Petal.Width", # 因变量
has.line = FALSE, # 无拟合线条
colours = "steelblue" # 颜色修饰
)
一个简洁风格的散点图就画好了, 无需使用太多代码。
nice_scatter(
data = iris,
predictor = "Sepal.Length", # 自变量
response = "Petal.Width" # 因变量
)
nice_scatter(
data = iris,
predictor = "Sepal.Length", # 自变量
response = "Petal.Width", # 因变量
group = "Species", # 多组别
has.shape = TRUE, # 点的类型
has.line = TRUE, # 线的类型
legend.title = "Species", # legend题目
has.confband = TRUE# 显示置信区间
)
nice_scatter(
data = iris,
predictor = "Sepal.Length",
response = "Petal.Width",
has.confband = TRUE,
has.r = TRUE, # 展示相关系数
has.p = TRUE # 展示p值
)
使用另一个函数nice_violin()来画小提琴图:
nice_violin(
data = iris,
response = "Sepal.Length",
group = "Species",
comp1 = "setosa",
comp2 = "virginica"
)
上述代码中的comp1和comp2,即指定想要比较的两个组别,自动展示*,**,或者***。对小提琴图进行一些修饰,并且想手动填写统计显著性:nice_violin(
data = iris,
response = "Sepal.Length",
group = "Species",
ytitle = "Length of Sepal",
xtitle = "Species of Iris",
colours = c("darkseagreen", "gold", "purple"),
ymin = 4, # y范围
ymax = 11,
yby = 2, # y轴刻度
signif_annotation = c("*", "NS", "***"), # 手动填入统计显著性
signif_yposition = c(8, 9, 10), # 统计结果的位置
signif_xmin = c(1, 2, 1),
signif_xmax = c(2, 3, 3),
CIcap.width = 0.1, # 置信区间宽度
alpha = 0.5, # 透明度
border.colour = "black"
)
nice_density(
data = iris,
variable = "Sepal.Length",
group = "Species",
title = ""
)
nice_density(
data = iris,
variable = "Sepal.Length",
group = "Species",
groups.labels = c("(a)", "(b)","(c)"),
title = "",
histogram = TRUE,
bins = 30
)
在检验数据正态性时,可以画个Q-Q图(Quantile-Quantile Plot):
nice_qq(
data = iris,
variable = "Sepal.Length"
)
不仅仅是轻松画出Q-Q图,{rempsyc}还贴心提供了专门的函数nice_normality()给大家:nice_normality(
data = iris,
variable = "Sepal.Length",
groups.labels = ""
)
最后,假设需要观察多个组别的数据正态性,也很容易:
nice_normality(
data = iris,
variable = "Sepal.Length",
group = "Species", # 多组别
colours = c("orange", "#619CFF","#F8766D"),
grid = FALSE,
shapiro = TRUE
)
好啦,今天的内容就到这里。如果有帮助,记得分享给需要的人![1]. Thériault, R. (2023). rempsyc: Convenience functions for psychology. Journal of Open Source Software, 8(87), 5466. https://doi.org/10.21105/joss.05466
【通过公众号菜单栏--线上课程--新手课程/回归课程】公众号核心成员担任SCI杂志Associate Editor!▌课程相关咨询可添加R师妹微信: kefu_rstats