R语言中如何利用ggplot2绘制qq图和boxplot图
文章目录
- 绘制qq图
- 函数介绍
- geom_qq()
- 参数介绍
- 注意事项
- 例子
- Using to explore the distribution of a variable
- 绘制boxplot
- 函数介绍
- 参数介绍
- 例子
- 利用分位点绘制箱线图
- 将qq图和箱线图进行融合
- 函数介绍
- 参数介绍
- 注意事项
- 例子
绘制qq图
在ggplot2中绘制qq图需要两步,geom_qq()
将绘制样本分位点,geom_qq_line()
将绘制标准正态线
函数介绍
geom_qq()
geom_qq(mapping = NULL,data = NULL,geom = "point",position = "identity",...,distribution = stats::qnorm,dparams = list(),na.rm = FALSE,show.legend = NA,inherit.aes = TRUE
)
geom_qq_line(mapping = NULL,data = NULL,geom = "path",position = "identity",...,distribution = stats::qnorm,dparams = list(),line.p = c(0.25, 0.75),fullrange = FALSE,na.rm = FALSE,show.legend = NA,inherit.aes = TRUE
)
参数介绍
**aes()**中的映射参数必须包含sample,可选参数有group,x,y distribution
Distribution function to use, if x not specified
dparams Additional parameters passed on to distribution function.
line.p Vector of quantiles to use when fitting the Q-Q line, defaults defaults to c(.25, .75).
fullrange Should the q-q line span the full range of the plot, or just the data
注意事项
**aes()**中的映射参数必须包含sample
例子
Using to explore the distribution of a variable
ggplot(mtcars, aes(sample = mpg)) +stat_qq() +stat_qq_line()
ggplot(mtcars, aes(sample = mpg, colour = factor(cyl))) +stat_qq() +stat_qq_line()
绘制boxplot
函数介绍
geom_boxplot(mapping = NULL,data = NULL,stat = "boxplot",position = "dodge2",...,outlier.colour = NULL,outlier.color = NULL,outlier.fill = NULL,outlier.shape = 19,outlier.size = 1.5,outlier.stroke = 0.5,outlier.alpha = NULL,notch = FALSE,notchwidth = 0.5,varwidth = FALSE,na.rm = FALSE,orientation = NA,show.legend = NA,inherit.aes = TRUE
)
参数介绍
aes()可接收的参数有:
x or y, 利用x将会是横向箱线图,y的是纵向
lower or xlower
upper or xupper
middle or xmiddle
ymin or xmin
ymax or xmax
alpha
colour
fill
group
linetype
shape
size
weight
notch If FALSE (default) make a standard box plot. If TRUE, make a notched box plot. Notches are used to compare groups; if the notches
of two boxes do not overlap, this suggests that the medians are
significantly different.notchwidth For a notched box plot, width of the notch relative to the body (defaults to notchwidth = 0.5).
varwidth If FALSE (default) make a standard box plot. If TRUE, boxes are drawn with widths proportional to the square-roots of the
number of observations in the groups (possibly weighted, using the
weight aesthetic).
例子
p <- ggplot(mpg, aes(x=class, y=hwy))
p + geom_boxplot()
ggplot(mpg, aes(x=hwy, y=class)) + geom_boxplot()
p <- ggplot(mpg, aes(x=class, y=hwy))
p + geom_boxplot(notch = TRUE,varwidth = TRUE,fill = "white", colour = "#3366FF")
ggplot(diamonds, aes(carat, price)) +geom_boxplot(aes(group = cut_width(carat, 0.25)))
p <- ggplot(mpg, aes(x=class, y=hwy))
p + geom_boxplot(outlier.shape = NA) + geom_jitter(width = 0.2)
利用分位点绘制箱线图
y <- rnorm(100)
df <- data.frame(x = 1,y0 = min(y),y25 = quantile(y, 0.25),y50 = median(y),y75 = quantile(y, 0.75),y100 = max(y)
)
ggplot(df, aes(x)) +geom_boxplot(aes(ymin = y0, lower = y25, middle = y50, upper = y75, ymax = y100),stat = "identity")
将qq图和箱线图进行融合
函数介绍
该函数是来自于qqboxplot
包,因此使用前需要安装
geom_qqboxplot(mapping = NULL,data = NULL,stat = "qqboxplot",position = "dodge2",...,outlier.colour = NULL,outlier.color = NULL,outlier.fill = NULL,outlier.shape = 19,outlier.size = 1.5,outlier.stroke = 0.5,outlier.alpha = NULL,notch = FALSE,notchwidth = 0.5,varwidth = FALSE,na.rm = FALSE,show.legend = NA,inherit.aes = TRUE
)
参数介绍
大部分参数和geom_qq()和geom_boxplot()中的参数含义相同
reference_dist 表示参数比较的标准分布名称,如果有参数需要有dparams
compdata 用于比较的标准样本数据,是个向量
注意事项
aes()函数中的y不可缺
例子
library(dplyr)
library(ggplot2)
library(qqboxplot)simulated_data=tibble(y=c(rnorm(1000, mean=2), rt(1000, 16), rt(500, 4), rt(1000, 8), rt(1000, 32)),group=c(rep("normal, mean=2", 1000), rep("t distribution, df=16", 1000), rep("t distribution, df=4", 500), rep("t distribution, df=8", 1000), rep("t distribution, df=32", 1000)))
p <- ggplot2::ggplot(simulated_data, ggplot2::aes(factor(group,levels=c("normal, mean=2", "t distribution, df=32", "t distribution, df=16","t distribution, df=8", "t distribution, df=4")), y=y))
p + geom_qqboxplot()
p + geom_qqboxplot(reference_dist = "norm")p + geom_qqboxplot(compdata = comparison_dataset)
R语言中如何利用ggplot2绘制qq图和boxplot图相关推荐
- R语言可视化:使用ggplot2绘制人口金字塔
人口金字塔是进行人口数据可视化时常用的一种统计图形,可以形象地描述人口年龄和性别的分布情况.最近工作上经常处理人口数据,于是试着使用ggplot2绘制了一下.在这里记录一下,顺便也熟悉一下ggplot ...
- 用r语言画出y = ax^2 + bx + c,R语言中使用curve函数绘制常用函数曲线
前面文章中介绍了使用plot函数绘制图形的方法.本文介绍一下使用R中curve函数绘制常见函数曲线的方法. 1.curve函数简介 curve函数语法格式如下: curve(expr, from = ...
- R语言中使用UpSet包绘制多集合Venn图
集合的交集一帮采用Venn图进行展示,但当集合数目超过3时,使用Venn图直接展示会显得比较杂乱,我们倾向于使用UpSet展示. 这里,我们介绍UpSet的概念,并展示如何使用UpSet packag ...
- R 语言中添加辅助线(ggplot2)
来源 绘制横跨整个坐标系的直线: 因为y轴是连续的,所以在这里我们使用 geomhline, 当然在x轴是连续的情况下也可以使用 geomvline (加入 xintercept参数) . 为不同分类 ...
- R语言中如何进行PCA分析?利用ggplot和prcomp绘制基因表达量分析图
学习笔记的主要内容是在R语言中利用ggplot2进行PCA分析和绘图,包括简单分析与操作流程,对比不同方式得到的结果差异,提供脚本代码供练习. PCA分析的原理 在处理基因差异表达数据时,有时候需要分 ...
- tmap | R语言中专门绘制地图的工具包
tmap工具包的名称是Thematic Maps的缩写,是R中专门绘制地图的工具包.该包语法与ggplot2包比较类似,都是通过符合+来进行图层叠加. 加载示例数据: library(tidyvers ...
- R语言中的基础作图和ggplot2配色系统
文章目录 颜色代码大全 R语言中的调色板 RColorBrewer提供的调色盘 R语言中配色介绍 R语言中自带的调色板 RColorBrewer包提供更多的调色板 ggplot2中配色系统的介绍 数值 ...
- R语言R原生及可视化包ggplot2绘制并排的箱图实战(Side-by-Side Boxplots)
R语言R原生及可视化包ggplot2绘制并排的箱图实战(Side-by-Side Boxplots) 目录 R语言R原生及可视化包ggplot2绘制并排的箱图实战(Side-by-Side Boxpl ...
- R语言可视化包ggplot2绘制Bump Chart(凹凸图)实战
R语言可视化包ggplot2绘制Bump Chart(凹凸图)实战 目录 R语言可视化包ggplot2绘制Bump Chart(凹凸图)实战 #导入包
最新文章
- android 耳机红外线,红外线耳机制作方法
- 恶犬秒变萌汪:东京大学开源“治愈系”GAN图片拼贴工具 | 技术头条
- 爬取百度百科上中国所有城市的信息
- PHP如何添加自带的扩展库
- 开启灯光就是近光吗_摩托车灯光你用对了吗?双闪的作用是什么?
- D2 日报 2019年4月17日
- linux跑循环脚本占内存,Linux下实现脚本监测特定进程占用内存情况
- Linq 左连接 left join
- vscode 构建Python ,和C++ 开发环境
- uclinux 嵌入式linux,嵌入式操作系统uCLinux
- opencv3和qt5计算机视觉应用开发pdf_NeurIPS 2019:计算机视觉论文回顾
- phpmyadmin安装配置以及相关问题
- Python的视频分析
- Error creating bean with name错误,spring-boot报错
- 2022电大国家开放大学网上形考任务-电子商务概论非免费(非答案)
- Windows聚焦壁纸
- html css 美化模板,通用css样式,全局css样式,css样式模板,
- Chrome 插件 CrxMouse 去除后门优化版
- 华盛顿大学计算机科学,华盛顿大学UW(University of Washington)计算机科学Computer Science专业排名第21位(2021年THE世界大学商科排名)...
- 通过V90PN通讯故障实例来看线路干扰与线路错误的区别