基本
基本元素、函数
基本元素
函数名 | 功能 |
---|---|
points(x,y) | 添加点 |
lines(x,y) | 添加线 |
text(x,y,labels,…) | 在(x,y)处添加用labels指定的文字 |
mtext(text,side=3,line=0,…) | 在边空添加用text指定的文字 |
segments(x0,y0,x1,y1) | 从(x0,y0)各点到(x1,y1)各点画线段 |
arrows(x0,y0,x1,y1,…) | 同上,但添加箭头 |
abline(a,b) | 绘制斜率为b和截距为a的直线 |
abline(h=y) | 在纵坐标y处画水平线, y可以是向量, abline(h=c(1,2,3)) |
abline(v=x) | 在横坐标x处画垂直线, x可以是向量 |
abline(lm.obj) | 画出lm.obj确定的回归线 |
rect(x1,y1,x2,y2) | 绘制长方形,(x1,y1)为左下角,(x2,y2)为右上角 |
polygon(x,y) | 绘制连接各x,y坐标确定的点的多边形 |
legend(x,y,legend) | 在点(x,y)处添加图例,说明内容由legend给定 |
title() | 添加标题,也可添加一个副标题 |
axis(side,vect) | 画坐标轴 |
box() | 在当前的图上加边框 |
rug(x) | 在x轴上用短线画出x数据的位置 |
locator(n,type=”n”,…) | 获取鼠标在图中点击处的坐标 |
常用绘图函数
函数名 | 功能 |
---|---|
plot(x) | 以x的元素值为纵坐标、以序号为横坐标绘图 |
plot(x,y) | x与y的二元作图 |
pie(x) | 饼图 |
boxplot(x) | 盒形图(也称箱线图) |
hist(x) | x的频率直方图 |
barplot(x) | x的值的条形图 |
pairs(x) | 如果x是矩阵或是数据框,作x的各列之间的二元图 |
matplot(x,y) | 二元图,其中x的第一列对应y的第一列,依次类推 |
qqnorm(x) | 正态分位数-分位数图 |
image(x,y,z) | x,y,z三元图 |
heatmap(x) | 热图 |
coplot(x~y|z) | 关于z的每个数值(或数值区间)绘制x与y的二元图 |
常见图,例子
柱状图、分类柱状图、簇形图、百分比堆积图、饼图极坐标图、折线图
安装包方法
|
|
几何性质相关
alpha, color, fill, linetype, shape, size
shape
linetype:
0 = blank, 1 = solid, 2 = dashed, 3 = dotted, 4 = dotdash, 5 = longdash, 6 = twodas
数据操作
矩阵操作
合并
取出部分行、列
截取、排序、构造新列
|
|
ggplot 跳过NA数据
stat, pos, binwidth, scale
|
|
标签
labs, guides
单独画坐标、竖线(admixture图里面)
将绘图区划分成 m 行 n 列
用facet把图分成小图,多图
|
|
facet_wrap()说明
mfrow方法
par(mfrow=c(m,n))
gridExtra方法,可以和ggplot一起使用
|
|
常用图例子
点图 point
应用(PCA)
input file:“input.ggplot2”:
geom_point()
|
|
|
|
特殊例子,同时设定颜色color和形状,同时只显示一个legend
在scale_colour_manual
和scale_shape_manual
中使用同样的name
和labels
link
手动指定颜色、形状,例子:
|
|
plot & points
|
|
barplot 柱状图
barplot
|
|
|
|
barplot for admixture results example:
|
|
|
|
geom_bar
给定数据的柱状图
http://blog.csdn.net/bone_ace/article/details/47267981
http://shujuren.org/article/44.html
geom_histogram()
|
|
|
|
小提琴图 geom_violin()
|
|
boxplot 箱线图
boxplot
|
|
geom_boxplot
|
|
点+erro bar
|
|
pheatmap 热图
输入格式 input.txt
例子
其他例子
ggplot 调整作图细节
保存文件
|
|
自动加label
|
|
添加注释
添加文本注释
|
|
添加数学表达式注释
|
|
添加线条注释
|
|
|
|
|
|
添加分割标记
|
|
长方形阴影
|
|
添加误差线
|
|
每个小平面增加注释,多小图
|
|
加x、y坐标名字;标题
|
|
x、y坐标反转
|
|
自定义顺序
|
|
自定义颜色
|
|
颜色组合
|
|
|
|
|
|
|
|
好看的颜色ColorBrewer
|
|
|
|
theme主题设置,X轴、Y轴操作
|
|
默认主题
- theme_classic,有x,y轴和没有网格线 #(这个不错)
- theme_bw,高对比度主题
- theme_gray,默认
- theme_light
- theme_linedraw,网格线变成黑实线
- theme_minimal,没有背景内容
- 更多 library(ggthemes)
theme关键词表
参数 | 设置内容 | 继承自 |
---|---|---|
line | 所有线属性 | |
rect | 所有矩形区域属性 | |
text | 所有文本相关属性 | |
title | 所有标题属性 | |
axis.title | 坐标轴标题 | text |
axis.title.x | x轴属性 | axis.title |
axis.title.y | y轴属性 | axis.title |
axis.text | 坐标轴刻度标签属性 | text |
axis.text.x | 属性和继承和前面类似,不再重复 | |
axis.text.y | ||
axis.ticks | 坐标轴刻度线 | line |
axis.ticks.x | ||
axis.ticks.y | ||
axis.ticks.length | 刻度线长度 | |
axis.ticks.margin | 刻度线和刻度标签之间的间距 | |
axis.line | 坐标轴线 | line |
axis.line.x | ||
axis.line.y | ||
legend.background | 图例背景 | rect |
legend.margin | 图例边界 | |
legend.key | 图例符号 | |
legend.key.size | 图例符号大小 | |
legend.key.height | 图例符号高度 | |
legend.key.width | 图例符号宽度 | |
legend.text | 图例文字标签 | |
legend.text.align | 图例文字标签对齐方式 | 0为左齐,1为右齐 |
legend.title | 图例标题 | text |
legend.title.align | 图例标题对齐方式 | |
legend.position | 图例位置 | left, right, bottom, top, 两数字向量 |
legend.direction | 图例排列方向 | “horizontal” or “vertical” |
legend.justification | 居中方式 | center或两数字向量 |
legend.box | 多图例的排列方式 | “horizontal” or “vertical” |
legend.box.just | 多图例居中方式 | |
panel.background | 绘图区背景 | rect |
panel.border | 绘图区边框 | rect |
panel.margin | 分面绘图区之间的边距 | |
panel.grid | 绘图区网格线 | line |
panel.grid.major | 主网格线 | |
panel.grid.minor | 次网格线 | |
panel.grid.major.x | ||
panel.grid.major.y | ||
panel.grid.minor.x | ||
panel.grid.minor.y | ||
plot.background | 整个图形的背景 | |
plot.title | 图形标题 | |
plot.margin | 图形边距 | top, right, bottom, left |
strip.background | 分面标签背景 | rect |
strip.text | 分面标签文本 | text |
strip.text.x | ||
strip.text.y |
加载字体
|
|
x、y轴,数据范围
|
|
坐标轴刻度间隔大小
|
|
检验
wilcox.test()
Wilcoxon rank sum test / Wilcoxon秩和检验
Mann–Whitney U test(曼-惠特尼U检验) / Mann–Whitney–Wilcoxon (MWW)
在R中,wilcox.test()函数可以用来做Wilcoxon秩和检验,也可以用于做 Mann-Whitney U检验。
- 当参数为
单个样本
/或者是两个样本相减
/或者是两个参数,paired=T
时,是Wilcoxon秩和检验。- 当
两个参数,paired = FALSE(独立样本)
时,就是 Mann-Whitney U检验If only x is given, or if both x and y are given and paired is TRUE, a Wilcoxon signed rank test of the null that the distribution of x (in the one sample case) or of x - y (in the paired two sample case) is symmetric about mu is performed.
Otherwise, if both x and y are given and paired is FALSE, a Wilcoxon rank sum test (equivalent to the Mann-Whitney test: see the Note) is carried out. In this case, the null hypothesis is that the distributions of x and y differ by a location shift of mu and the alternative is that they differ by some other location shift (and the one-sided alternative “greater” is that x is shifted to the right of y).
配对Wilcoxon符号秩检验
例8-1
对12份血清分别用原方法(old, 检测时间20分钟)和新方法(new, 检测时间10分钟)测谷-丙转氨酶。问两法所得结果有无差别?
独立样本Wilcoxon检验(Mann-Whitney U检验)
例8-2
已知某地正常人尿氟含量的中位数为45.30μmol/L 。今在该地某厂随机抽取12名工人,测得尿氟含量。问该厂工人的尿氟含量是否高于当地正常人的尿氟含量?
例8-3
对10例肺癌病人和12例矽肺0期工人用X光片测量肺门横径右侧距RD值(cm)。问肺癌病人的RD值是否高于矽肺0期工人的RD值?
例8-4
已知39名吸烟工人和40名不吸烟工人的碳氧血红蛋白HbCO(%)含量,问吸烟工人的HbCO(%)含量是否高于不吸烟工人的HbCO(%)含量?
答:本题书中用的是wilcox检验,其实用Ridit分析更合适一些,下面分别用这两种方法进行检验:
- Wilcox.test,或Kruskal检验
Wilcox.test(或Kruskal检验)
|
|
- 此题用Ridit检验更合适一些
|
|
Kurskal-Wallis检验
Kurskal-Wallis检验是Wilcoxon方法(其实是Mann-Whitney检验)用于多于两个样本的时候的升级版。当对两个样本进行比较的时候,Kurskal-Wallis检验与Mann-Whitney检验是等价的。
例8-5
用三种药物杀灭钉螺,每批用200只活钉螺,用药后清点每批钉螺的死亡数、再计算死亡率(%),问三种药物杀灭钉螺的效果有无差别?
|
|
例8-6
比较小白鼠接种三种不同菌型伤寒杆菌9D、11C和DSC1后存活日数,问小白鼠接种三种不同菌型伤寒杆菌的存活日数有无差别?
|
|
例8-7
四种疾病患者痰液内嗜酸性白细胞的检查结果见表8-11。问四种疾病患者痰液内的嗜酸性白细胞有无差别?注:这道例题与《医学统计学及SAS应用》(上海交通大学)的9.11类似
|
|
例8-8
对例8-6资料作三个样本间的两两比较,Nemenyi检验。
例8-9
已知8名受试对象在相同实验条件下分别接受4种不同频率声音的刺激,他们的反应率(%)资料。问4种频率声音刺激的反应率是否有差别?
|
|
手动计算PCA
prcomp
输入格式:
|
|