R语言_基本图形

attach(mtcars)
names(mtcars)
# "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear" "carb"

#条形图
#单向量条形图
barplot(table(cyl),
        main="main",xlab="x",ylab="y")
barplot(table(cyl),horiz = TRUE)
plot(as.factor(cyl))
plot(factor(cyl,levels = c(6,4,8)))
#堆砌与分组条形图
counts = table(cyl,gear)
barplot(counts,
        xlab="gear",ylab="frequency",
        col = c("red","yellow","green"),
        legend=rownames(counts))
barplot(counts,
        xlab="gear",ylab="frequency",
        col = c("red","yellow","green"),
        legend=rownames(counts),
        beside=TRUE)

legend(locator(1),title("title"),
       rownames(counts),
       lty=c(1,2),pch=c(15,17),
       col=c("red","yellow","green"))
#均值条形图
means = aggregate(mpg,by=list(cyl),mean)
means = means[order(means$x,decreasing = TRUE),]
barplot(means$x,names.arg=means$Group.1)
title("Mean Rate")
lines(means$x,)
#条形图微调
opar = par(no.readonly=TRUE)
par(mar=c(5,8,4,2))
par(las=1)
counts = table(cyl)
barplot(counts,
        main="mtcars cyl",
        horiz=TRUE,
        cex.names=2,
        names.arg=names(table(cyl)))
par(opar)
#棘状图
library(vcd)
counts = table(cyl,gear)
spine(counts,main="Spinogram Example")


#饼图
par(mfrow=c(2,2))
slices = c(10,12.4,16,8)
labels = c("1","2","3","4")
pie(slices,labels)
#add propotion
pct = round(slices/sum(slices)*100)
labels = paste(labels," ",pct,"%",sep="")
pie(slices,labels,col=rainbow(length(pct)))
#3d figure
install.packages("plotrix")
library(plotrix)
pie3D(slices,labels=labels,explode=0.1)
#build form table
counts = table(cyl)
lbls = paste(names(counts),"\n",counts,sep="")
pie(counts,labels=lbls)
par(opar)
#更加直观的扇形图
library(plotrix)
fan.plot(slices,labels=labels)
par(opar)


#直方图
#条状图一般偏向离散变量,直方图偏向连续变量
par(mfrow=c(2,2))
#1
hist(mpg)
#2 add breaks and color
hist(mpg,
     breaks=12,
     col="red",
     )
#3 add jitter, plot according to density rather than frequency
hist(mpg,
     breaks=12,
     col="red",
     freq=FALSE 
)
rug(jitter(mpg))
lines(density(mpg),col="blue",lwd=2)
#4 add norm curve and framework
x = mpg
h = hist(mpg,
     breaks=12,
     col="red", 
)
xfit = seq(min(x),max(x),length=40)
yfit = dnorm(xfit,mean=mean(x),sd=sd(x))
yfit1 = yfit*diff(h$mids[1:2])*length(x)
lines(xfit,yfit1,col="blue",lwd=2)
box()
par(opar)


#核密度图
#估计随机变量概率密度的非参数方法
#1
plot(density(mpg))
#2
par(mfrow=c(2,1))
d = density(mpg)
plot(d)
plot(d)
polygon(d,col="red",border="blue")
rug(mpg,col="brown")
par(opar)
#3 根据因子分组对连续变量密度图作比较
par(lwd=2)
library(sm)
cyl.f = factor(cyl,
               levels=c(4,6,8),
                labels=c("4cyl","6cyl","8cyl"))
sm.density.compare(mpg,cyl,
                   xlab="miles per gallon")
colfill = c(2:(1+length(levels(cyl.f))))
legend(locator(1),levels(cyl.f),fill=colfill)
legend(locator(1),levels(cyl.f),fill=rainbow(3))


#箱线图
#1
boxplot(mpg)
#2
boxplot(mpg~cyl,data=mtcars)
#3
#箱线图的宽度和样本大小成正比,凹槽不重叠表示中位数有显著差异
boxplot(mpg~cyl,data=mtcars,
        varwidth=TRUE,
        notch=TRUE)
#4
#两个因子交叉的箱线图
cyl.f = factor(cyl,levels=c(4,6,8))
am.f  = factor(am,levels=c(0,1),labels=c("auto","stand"))
boxplot(mpg~am.f*cyl.f,
        varwidth=TRUE,
        col=c("gold","darkgreen"))
#5
#小提琴图,结合了箱线图和核密度图
library(vioplot)
vioplot(mpg[cyl==4],mpg[cyl==6],mpg[cyl==8],col="gold",
        names=c("4","6","8"))
title("main",xlib="x",ylab="y")


#点图
#1 easy case
dotchart(mpg,labels=rownames(mtcars),cex=0.5)
#2 group sort and colored
x = mtcars[order(mtcars$mpg),]
x$cyl = factor(x$cyl)
x$color = ifelse(x$cyl==4,"red",
                 ifelse(x$cyl==6,"blue","darkgreen"))
dotchart(x$mpg,
         labels=rownames(x),cex=0.5,
         groups=x$cyl,color=x$color,
         pch=19)
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值