R 基础绘图体系-基础篇

1.高水平绘图函数

生成数据

#模拟100位同学学号及三科成绩
num = seq(12340001,12340100) # 形成学号
x1 = round(runif(100,min = 80,max = 100)) #随机产生max100和min80的100位同学科目1成绩
x2 = round(rnorm(100,mean = 80,sd = 5)) #随机产生mean80和sd5的100位同学科目1成绩
x3 = round(rnorm(100,mean = 85,sd = 17)) #随机产生mean85,sd17的100位同学科目1成绩
x3[which (x3 > 100)] = 100 #数值替换
x = data.frame(num,x1,x2,x3) #原始数据

直方图

hist(x$x3) #科目3各个成绩的人数分布

散点图

plot(x1,x3) #科目1与科目3的散点图(两科关系)

配对散点图

pairs(x[2:4],col = "blue")

有因子影响的配对散点图

coplot(x1~x2|x3,col = 1:12,pch = 17) #以x3作为分类因子

柱状图

table(x$x3) #获取列联表,统计各成绩的人数
barplot(table(x$x3)) #绘制科目3分布情况


柱状图与直方图反馈信息基本一致,但直方图横坐标通常分段统计频数,柱状图横坐标为单个样本频数,故,直方图可用于横坐标为数值类型(numeric)的数据,柱状图用于横坐标为字符串类型(character)的数据。

饼图

pie(table(x$x3)) #绘制科目三成绩占比

箱线图

boxplot(x$x1,x$x2,x$x3,col = 2:4,notch = T) 

绘制三科成绩的分散分布(最小值,25%分位数,50%分位数【中位数】,75%分位数,最大值)。孤立值有时需校检是否为错误值。

# 横向绘制,加参数 horizontal = T
boxplot(x$x1,x$x2,x$x3,col = 2:4,notch = T,horizontal = T)

雷达图

stars(x[2:4],col.stars = rep(c(1:10),10),labels = x$num,full = T,cex = 0.3)  #绘制学生成绩的优势,是否偏科

# 变为扇形,加参数 draw.segments =T

qq图

qqnorm(x$x1) 
qqline(x$x1) #判断学生成绩是否为正态分布,可见 科目2符合正态分布


等高线图

y = -19:22 
contour(outer(x, x), method = "edge") #简单的等高线图

z = outer(y,y)
image(y,y,z)
contour(y,y,z,method = "edge",add = T,col = "blue3",lwd = 2) # 将等高线加到渐变图上

3D 等高线图

persp(y,y,z,theta = 0,phi = 15,col = "green3",border = NA,shade = 0.6,nticks = T) 


theta和phi控制数据角度,当前为缺省值。border=NULL可以显示界面网格。shade加入阴影,col添加颜色。par(mfrow = c(2,2))设置图像呈2行2列排布。

par(mfrow=c(2,2))
persp(y,y,z,theta = 0,phi = 15,col = "gold3",border = NA,shade = 0.6,nticks = T)
persp(y,y,z,theta = 0,phi = 15,col = "red3",border = NA,shade = 0.6,nticks = T)
persp(y,y,z,theta = 0,phi = 15,col = "blue3",border = NA,shade = 0.6,nticks = T)
persp(y,y,z,theta = 0,phi = 15,col = "purple3",border = NA,shade = 0.6,nticks = T)

表状点图

data(VADeaths) # 佛尼吉亚州1940年的人口死亡率
dotchart(t(VADeaths),lcolor = 2:6,pt.cex = 1.1,cex = 0.5,bg = 3,pch = 16) #城乡间各年龄段的死亡率
dotchart(t(VADeaths),lcolor = 2:5,pt.cex = 1.1,cex = 0.5,bg = 3,pch = 16) #不同年龄段在城乡间的死亡率


高水平作图函数的参数命令

1)逻辑命令
add = T or F, 即是否直接在已有图形上增加新的图层
axes = T or F,即是否显示坐标轴
2)数据取对数
log = "x", "y" or "xy", 表示分别对x轴,y轴或xy轴取对数
3)type 命令
type = "p" #绘制散点图
type = "l" #绘制线图
type = "b" #用实线连接所有点
type = "o" #实线通过所有的点
type = "h" #绘制点到x轴的竖线,棒棒糖图可用
type = "s" or "S" #绘制阶梯型曲线
type = "n" #不绘制任何点或曲线

 plot(x$x1,type = "p",col = 4,main = "type = \"p\"")
 plot(x$x1,type = "l",col = 4,main = "type = \"l\"")
 plot(x$x1,type = "b",col = 4,main = "type = \"b\"")
 plot(x$x1,type = "o",col = 4,main = "type = \"o\"")
 plot(x$x1,type = "h",col = 4,main = "type = \"h\"")
 plot(x$x1,type = "s",col = 4,main = "type = \"s\"")
 plot(x$x1,type = "S",col = 4,main = "type = \"S\"")
 plot(x$x1,type = "n",col = 4,main = "type = \"n\"")


4)图中字符串
xlab or ylab = 字符串 #x轴或y轴说明
main = 字符串 #图说明,或为图主标题
sub = 字符串 #子图说明,子图标题

2.低水平作图函数

有时高水平作图不能完全显示所有信息,需要低水平作图予以辅助,即在高水平作图函数的基础上作图,增加新图形。

加点或线

plot(x$x1)
lines(x$x2,col = 2)
lines(x$x3,col = 4)

plot(x$x1)
points(x$x2,col = 2)
points(x$x3,col = 4)

加样本标记

plot(x$x1)
text(x$x1,col = 4,pos = 1) #pos 位置1,2,3,4分别对应下,左,上,右

加直线或多边形

data("Iris")
plot(iris$Sepal.Length,iris$Petal.Length,col = 4,main = "iris",xlab = "Sepal Length",ylab = "Petal Length")
abline(h = 2.5) #绘制 y = 2.5
abline(v = 5.5) #绘制 x = 5.5
abline(-6.0,1.7, col = "green") #绘制 y = -6.0 + 1.7x
lm.obj = lm(iris$Petal.Length ~ iris$Sepal.Length, data = iris) #构建线性回归模型
abline(lm.obj,col = "red") #绘制线性回归曲线
text(6.5,6,"y = -7.101 + 1.858x",col = "red",cex = 1) #添加回归拟合方程式
polygon(c(6,8,8,6),c(2,2,0.9,0.9),border = T,col = "gold") #在空白区域产生一个形状。

形状坐标的为前后两个数组各取一个组成,前一数组取值对应x轴,后一数组对应y轴。当前方格顶点坐标分别为(6,2),(8,2),(8,0.9),(6,0.9)

posted @ 2020-05-23 12:19  真小兽医  阅读(357)  评论(0编辑  收藏  举报