R中级绘图

#散点图
attach(mtcars)
plot(wt,mpg,main="Test1",xlab="wt",ylab="mpg",pch=19)
#添加最佳拟合的线性直线
abline(lm(mpg~wt),col="red",lwd=2,lty=1)
#添加平滑曲线lowess()和loess()
lines(lowess(wt,mpg),col="blue",lwd=2,lty=2)


#car包中的scatterplot()
library(car)
#mpg~wt|cyl表示在cyl各个水平下分别绘制mpg和wt的关系图
scatterplot(mpg~wt|cyl,data=mtcars,lwd=2,main="Test2",xlab="x",ylab="y",legend.plot=TRUE,
            id.method="identify",labels=row.names(mtcars),boxplots="xy")


#散点图矩阵
pairs(~mpg+disp+drat+wt,data=mtcars,main="Test3")


scatterplot.matrix(~mpg+disp+drat+wt,data=mtcars,spread=TRUE,lty.smooth=2,main="Test4")
scatterplot.matrix(~mpg+disp+drat+wt|cyl,data=mtcars,spread=TRUE,diagonal="histogram",main="Test5")


library(gclus)#可以使相关性更高的变量跟靠近主对角线
mydata<-mtcars[c(1,3,5,6)]
mydata.corr<-abs(cor(mydata))
mycolors<-dmat.color(mydata.corr)
myorder<-order.single(mydata.corr)
cpairs(mydata,myorder,panel.colors = mycolors,gap=.5,main="Test5")


#高密度散点图
set.seed(1234)
n<-10000
c1<-matrix(rnorm(n,mean=0,sd=0.5),ncol=2)
c2<-matrix(rnorm(n,mean=3,sd=2),ncol=2)
mydata<-rbind(c1,c2)
mydata<-as.data.frame(mydata)
names(mydata)<-c("x","y")
with(mydata,plot(x,y,pch=19,main="Test6"))


#用颜色密度来显示重叠的数目
with(mydata,smoothScatter(x,y,main="Test7"))


#六边形单元格显示
library(hexbin)
with(mydata,{bin<-hexbin(x,y,xbins=50)
plot(bin,main="Test8")})


#颜色展示密度变化
library(IDPmisc)
with(mydata,iplot(x,y,main="Test9"))


#三维散点图
library(scatterplot3d)
attach(mtcars)
scatterplot3d(mtcars$wt,mtcars$disp,mtcars$mpg,main="Test10")


#气泡图--通过点的大小来比较(商业应用中用得很多)
attach(mtcars)
r<-sqrt(disp/pi)
symbols(wt,mpg,circle=r,inches = 0.3,fg="white",bg="lightblue",main="Test11",xlab="x",ylab="y")
text(wt,mpg,rownames(mtcars),cex=0.6)
detach(mtcars)


#折线图
plot(x=,y=,type=)
lines(x=,y=,type=)

Orange$Tree<-as.numeric(Orange$Tree)
ntrees<-max(Orange$Tree)
x<-range(Orange$age)
y<-range(Orange$circumference)
plot(x,y,type="n",xlab="x",ylab="y")


colors<-rainbow(ntrees)
linetype<-c(1:ntrees)
plotchar<-seq(18,18+ntrees,1)


for(i in 1:ntrees)
{
  tree<-subset(Orange,Tree==i)
  lines(tree$age,tree$circumference,type="b",lwd=2,lty=linetype[i],col=colors[i],pch=plotchar[i])
}


legend(x[1],y[2],1:ntrees,cex=0.8,col=colors,pch=plotchar,lty=linetype,title="Tree")


#相关图


library(corrgram)
corrgram(mtcars,order=TRUE,lower.panel = panel.shade,upper.panel = panel.pie,text.panel = panel.txt,
         main="Test12")


corrgram(mtcars,order=TRUE,lower.panel = panel.ellipse,upper.panel = panel.pts,text.panel = panel.txt,
         
         diag.panel = panel.minmax , main="Test12")


#马赛克图---多类别型变量的频数、频率展现
library(vcd)
ftable(Titanic)
mosaic(Titanic,shade=TRUE,legend=TRUE)
mosaic(~Class+Sex+Age+Survived,data=Titanic,shade=TRUE,legend=TRUE)
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值