R/Python数据-R语言-图表-决策-Linux-Python

R语言笔记-3-绘图

2016-12-06  本文已影响84人  茶苯海
plot(faithful)
short.eruptions<-with(faithful,faithful[eruptions<3,])
#把喷发时间不超过3分钟的用红色点标出来
points(shorts.eruptions,col="red",pch=19)


#绘制一条紫色的垂直线
abline(v=3,color="purple")
#向数据图添加直线(回归)
fit<-lm(waiting~eruptions,data=faithful)
lines(faithful$eruptions,fitted(fit),col="blue")
#或者用
abline(a=coef(fit)[1],b=coef(fit)[2])
abline(fit,col="red")
plot(LakeHuron,type="l",main='type="l"') #折线图
plot(LakeHuron,type="p",main='type="p"') #散点图
plot(LakeHuron,type="l",main='type="b"') #点线图
with(mtcars,plot(mpg,disp))
with(mtcars,boxplot(disp,mpg))
with(mtcars,hist(mpg)
pairs(iris)       #绘制散点图矩阵
old.par<-par(mfrow=c(1,2))
plot(faithful,main="Faithful eruptions")
plot(large.islands,main="Islands",ylab="Area")
par(old.par)  #恢复摄者

home文件夹 "~/"

png(filename="faithful.png")   #打开一个图形设备
plot(faithful)     #创建数据集
dev.off()          关闭图形设备

#绘制散点图,使用xyplot()函数
#formula: y~x |z 创建 y对x的数据图,以z为条件
xyplot(mpg ~ hp | factor(cyl), data=mtcars)
barchart(car ~ mpg |factor(cyl),data=mtcars,
main="barchart",
scales=list(cex=0.5),
layout=c(3,1)

绘制箱型图

bwplot(~ hp |factor(cyl) ,data=mtcars,main="bwplot")

绘制分组数据

mtcars$cars <- rownames(mtcars)
mtcars$am <- with (mtcars, ifelse(am=0, "Automatic","Manual"))
barchart(cars ~ mpg | factor(cyl), data=mtcars,group=am,scales=list(cex=0.5),layout=c(3,1),
auto.key=TRUE)#添加图例
上一篇 下一篇

猜你喜欢

热点阅读