R语言实验四Word文件下载.docx
《R语言实验四Word文件下载.docx》由会员分享,可在线阅读,更多相关《R语言实验四Word文件下载.docx(60页珍藏版)》请在冰豆网上搜索。
p"
"
l"
b"
c"
o"
h"
s"
S"
n"
))#9种类型
{plot(speed,dist,type=i,
main=paste("
type=\"
"
i,"
\"
sep="
))}#\"
为双引号
detach()#取消连接数据集
4.2
df<
data.frame(Age=c(13,13,14,12,12,15,11,15,14,14,14,15,12,13,12,16,12,11,15),Height=c(144,166,163,143,152,169,130,159,160,175,161,170,146,159,150,183,165,146,169),Weight=c(38.1,44.5,40.8,34.9,38.3,50.8,22.9,51.0,46.5,51.0,46.5,60.3,37.7,38.1,45.1,68.0,58.1,38.6,50.8))#数据框
pairs(df)#多组图
pairs(~Age+Height+Weight,data=df)#与上述结果相
4.3
coplot(Weight~Height|Age,data=df)#年龄条件下的协同图
4.4点图
VADeaths#Virginia州在1940年的人口死亡率数据(R自带)
me1<
-apply(VADeaths,1,mean)#矩阵的行向量的均值
me2<
-apply(VADeaths,2,mean)#矩阵的列向量的均值
dotchart(VADeaths,gdata=me2,gpch=19,#按类型分类
main="
DeathRatesinVirginia-1940"
)
dotchart(t(VADeaths),gdata=me1,gpch=19,#按年龄分类
4.5饼图
pie.sales<
-c(39,200,42,15,67,276,27,66);
names(pie.sales)<
-c("
EUL"
"
PES"
EFA"
EDD"
ELDR"
EPP"
UNE"
other"
)#各候选人的得票结果
##figure1,默认色彩,逆时针
pie(pie.sales,radius=0.9,main="
Ordinarychart"
##figure2,彩虹色彩,顺时针
pie(pie.sales,radius=0.9,col=rainbow(8),clockwise=TRUE,main="
Rainbowcolours"
##figure3,灰度色彩,顺时针
pie(pie.sales,radius=0.9,clockwise=TRUE,col=gray(seq(0.4,1.0,length=8)),main="
Greycolours"
##figure4,阴影色彩,逆时针
pie(pie.sales,radius=0.9,density=10,angle=15+15*1:
8,main="
Thedensityofshadinglines"
4.6条形图
par(mai=c(0.9,0.9,0.3,0.3))#定义图像边距
##figure1,添加一条线
r<
barplot(pie.sales,space=1,col=rainbow(8));
lines(r,pie.sales,type='
h'
col=1,lwd=2)
##figure2,用text()添加平均值
mp<
-barplot(VADeaths);
tot<
-colMeans(VADeaths);
text(mp,tot+3,format(tot),xpd=TRUE,col="
blue"
)#
##figure3,添加条形的颜色
barplot(VADeaths,space=0.5,col=c("
lightblue"
mistyrose"
lightcyan"
lavender"
cornsilk"
))
##figure4,条形平行排列
barplot(VADeaths,beside=TRUE,col=c("
),legend=rownames(VADeaths),ylim=c(0,100))
4.7直方图
par(mai=c(0.9,0.9,0.6,0.3))#图形边距
attach(df)#连接数据框
##figure1,增加直方图和外框的颜色,以及相应的频数
hist(Height,col="
border="
red"
labels=TRUE,
ylim=c(0,7.2))
##figure2,使用线条阴影并利用text()标出频数,用lines()绘出数据的密度曲线(蓝色)和正态分布密度曲线(红色)
-hist(Height,breaks=12,freq=FALSE,density=10,angle=15+30*1:
6);
text(r$mids,0,r$counts,adj=c(.5,-.5),cex=1.2);
lines(density(Height),col="
lwd=2);
x<
-seq(from=130,to=190,by=0.5);
lines(x,dnorm(x,mean(Height),sd(Height)),col="
lwd=2)
detach()#取消连接数据框
4.8箱线图
(1)
c(25,45,50,54,55,61,64,68,72,75,75,78,79,81,83,84,84,84,85,86,86,86,87,89,89,89,90,91,91,92,100)
fivenum(x)#上、下四分位数,中位数,最大和最小值
boxplot(x)#绘制箱线图
(2)
InsectSprays#数据框,其中count为昆虫数目,spray为杀虫剂的类型
boxplot(count~spray,data=InsectSprays,col="
lightgray"
#矩形箱线图
boxplot(count~spray,data=InsectSprays,notch=TRUE,col=2:
7,add=TRUE)
4.9QQ图
-data.frame(Age=c(13,13,14,12,12,15,11,15,14,14,14,15,12,13,12,16,12,11,15),Height=c(144,166,163,143,152,169,130,159,160,175,161,170,146,159,150,183,165,146,169),Weight=c(38.1,44.5,40.8,34.9,38.3,50.8,22.9,51.0,46.5,51.0,46.5,60.3,37.7,38.1,45.1,68.0,58.1,38.6,50.8))#数据框
par(mai=c(0.9,0.9,0.6,0.3))
attach(df)
qqnorm(Weight)#数据的正态Q-Q图
qqline(Weight)#在Q-Q图上增加一条理论直线y=σx+μ
qqnorm(Height)
qqline(Height)
detach()
4.10三维透视图—persp
y<
-x<
-seq(-7.5,7.5,by=0.5)#定义域
f<
-function(x,y){r<
-sqrt(x^2+y^2)+2^{-52}#加上一个很小的量2^{-52}是为了避免在下一行运算时分母为零
z<
-sin(r)/r};
-outer(x,y,f)#对f作外积运算形成网格
par(mai=c(0.0,0.2,0.0,0.1))#图像边距
persp(x,y,z,theta=30,phi=15,expand=.7,col="
xlab="
X"
ylab="
Y"
zlab="
Z"
)#绘制三维图
4.11等值线—contour
y<
-x<
-seq(-3,3,by=0.125)#定义域
-function(x,y){z<
-3*(1-x)^2*exp(-x^2-(y+1)^2)-10*(x/5-x^3-y^5)*exp(-x^2-y^2)-1/3*exp(-(x+1)^2-y^2)};
z<
-outer(x,y,f)#对函数f作外积运算形成网格
par(mai=c(0.8,0.8,0.2,0.2))#图像边距
contour(x,y,z,levels=seq(-6.5,8,by=0.75),xlab="
col="
)#绘制等值线
4.12添加点、线、文字或符号
data(iris)#调用数据
op<
-par(mai=c(1,1,0.3,0.3),cex=1.1)#定义图形参数
-iris$Petal.Length;
-iris$Petal.Width
plot(x,y,type="
PetalLength"
PetalWidth"
cex.lab=1.3)
Species<
setosa"
versicolor"
virginica"
pch<
-c(24,22,25)#图中点的形状
for(iin1:
3){index<
-iris$Sp