R语言基本图形

##基本图形
#条形图
counts=table(Arthritis$Improved)
counts
#简单条形图
barplot(counts,main="simple bar plot",xlab="improvement",ylab="Frequency")
#水平条形图
barplot(counts,main="horizontal bar plot",xlab="frequence",ylab="improvement",horiz=TRUE)
#若要绘制的类别型变量是一个因子或者有序型因子,就可以用plot()
plot(Arthritis$Improved,horiz=TRUE,main="simple bar plot",xlab="improved",ylab="frequence")
#堆砌条形图和分组条形图
counts=table(Arthritis$Improved,Arthritis$Treatment)
counts
barplot(counts,main="Stack bar plot",xlab="treatment",ylab="frequency",
        col=c("red","yellow","green"),legend=rownames(counts))
barplot(counts,main="Grouped Bar Plot",xlab="treatment",ylab="frequency",
        col=c("red","yellow","green"),legend=rownames(counts),beside=TRUE)
#均值条形图
states=data.frame(state.region,state.x77)
means=aggregate(states$Illiteracy,by=list(state.region),FUN=mean)
means
means=means[order(means$x),]#将均值从小到大排序
means
barplot(means$x,names.arg = means$Group.1)
title("Mean Illiteracy Rate")#添加标题,与plot里的main等价
##棘状图
attach(Arthritis)
counts=table(Treatment,Improved)
spine(counts,main="Spinogram Example")
detach(Arthritis)
#饼图pie(x,labels)
par(mfrow=c(2,2))
slices=c(10,12,4,16,8)
lbls=c("US","UK","Australia","Germany","France")
#以上工作将四幅图组合为一幅图
pie(slices,labels=lbls,main="Simple Pie Chart")
pct=round(slices/sum(slices)*100)
lbls2=paste(lbls," ",pct,"%",seq=" ")
pie(slices,labels=lbls2,col=rainbow(length(lbls2)),main = 
      "pie chart with percentage")
pie3D(slices,labels=lbls,explode=0.1,
      main="3D pie chart")
##从表格创建饼图
mytable=table(state.region)
lbls3=paste(names(mytable),"\n",mytable,seq="")
pie(mytable,labels = lbls3,
    main="pie chart from a table\n (with sample sizes)")
#扇形图
slices=c(10,12,4,16,8)
lbls=c("US","UK","Australia","Germany","France")
fan.plot(slices,labels = lbls,main="Fan plot")
#直方图
#hist()
par(mfrowc(2,2))
hist(mtcars$mpg,breaks=12,col="red",xlab="Miles per Gallon",
     main="Colored histogram with 12 bins")
##添加轴须图
hist(mtcars$mpg,freq=FALSE,breaks=12,col="red",xlab="Miles per Gallon",
     main="Histogram,rug plot,density curve")
rug(jitter(mtcars$mpg))
lines(density(mtcars$mpg),col="blue","lwd=2")
##核密度图
par(mfrow=c(2,1))
d=density(mtcars$mpg)
plot(d)
d=density(mtcars$mpg)
plot(d,main="Kernel Density of Miles Per Gallon")
polygon(d,col="red",border="blue")
rug(mtcars$mpg,col="brown")
#箱型图boxplot(),点图dotchart()
 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值