R语言_图形初阶

#输出图形到pdf
pdf("mygraph.pdf")
attach(mtcars)
plot(wt,mpg)
abline(lm(mpg~wt))
title("regression of mpg on weight")
detach(mtcars)
dev.off()


#图形参数par设置
opar = par(no.readonly=TRUE)
par(lty=2,pch=17)   #指定线型2和符号形状17
par(lwd=2,cex=1.5)  #指定线宽2,符号大小1.5
par(pin=c(2,3))     #2英寸宽,3英寸高
plot(dose,drugA,type="b",
     main="main",sub="sub",
     xlab="xlab",ylab="ylab",
     xlim = c(0,60),ylim=c(0,70))
par(opar)


#图形颜色种类
n = 10
color = rainbow(10)
color = gray(0:n/n)
color = heat.colors(10)
pie(rep(1,n),labels=color,col=color)


#图形文字设置
title(main=,col.main=,
      sub=,col.sub=,
      xlab=,ylab=,
      col.lab=,cex.lab=)
axis(side=,at=,labels=,pos=,lty=,col=,las=,tck=,) #side的方向是下左上右
text(locator(1),"text to place",cex=,col=)
mtext("y=1/x",side=4,line=3,cex.lab=1,las=2,col="blue")
help(plotmath)                                    #数学文字


#综合1:左右两边两个y轴的图形设置
x = c(1:10)
y = x
z = 10/x
opar = par(no.readonly=TRUE)
par(mar=c(5,4,4,8)+0.1)
plot(x,y,type="b",
     pch=12,col="red",
     yaxt="n",lty=3,ann=FALSE)
lines(x,z,type="b",pch=22,col="blue",lty=2)
axis(2,at=x,labels=x,col.axis="red",las=2)
axis(4,at=z,labels=round(z,digits=2),
     col.axis="blue",las=2,cex.axis=0.7,tck=-0.01)
mtext("y=1/x",side=4,line=3,cex.lab=1,las=2,col="blue")
title("main",xlab="x",ylab="y")
legend("topleft",inset=0.05,title="title",
       c("a","b"),
         lty=c(1,2),pch=c(15,17),col=c("red","blue"))
legend(locator(1),title="title",
       c("a","b"),
       lty=c(1,2),pch=c(15,17),col=c("red","blue"),cex=0.5)
install.packages("Hmisc")
library(Hmisc)
minor.tick(nx=3,ny=3,tick.ratio=0.5)
par(opar)


#综合2:数据点右侧文字设置
with(mtcars,{plot(wt,mpg,,yaxt="n",ann=FALSE)})
with(mtcars,text(wt,mpg,row.names(mtcars),cex=0.6,pos=4,col="red"))


#综合3:多幅图片设置
#例子1
opar = par(no.readonly=TRUE)
par(mfrow=c(2,2))
with(mtcars,{
    plot(wt,mpg)
    plot(wt,disp)
    hist(wt)
    boxplot(wt)
})
par(opar)
#例子2
par(mfrow=c(3,1))
with(mtcars,{
    hist(wt)
    hist(mpg)
    hist(disp)
})
par(opar)
#例子3
layout(matrix(c(1,1,2,3),2,2,byrow = TRUE),
       widths=c(3,1),heights=c(1,2))
with(mtcars,{
    hist(wt)
    hist(mpg)
    hist(disp)
})
par(opar)
#例子4
par(fig=c(0,0.8,0,0.8))
with(mtcars,{
    plot(wt,mpg)
})
par(fig=c(0,0.8,0.55,1),new=TRUE)
boxplot(mtcars$wt,horizontal = TRUE,axes=FALSE)
par(fig=c(0.65,1,0,0.8),new=TRUE)
boxplot(mtcars$mpg,horizontal = FALSE,axes=FALSE)
mtext("main",side=3,outer=TRUE,line=-3)

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券

年度创作总结 领取年终奖励