(1)条形图
> barplot(c(1,2,4,2,6,4,3,5))
> barplot(c(1,2,4,2,6,4,3,5),horiz = TRUE)
#beside=TRUE 表示将多个组别的图形并排显示,使它们在水平方向上对齐
#而当 beside=FALSE(默认值)时,多个组别的图形会堆叠在一起
> data <- matrix(c(4, 5, 2, 6, 3, 7), nrow = 2)
> colnames(data) <- c("Group A", "Group B", "Group C")
> barplot(data, beside = TRUE)
>
> data
Group A Group B Group C
[1,] 4 2 3
[2,] 5 6 7
> library(vcd)
载入需要的程辑包:grid
> data("Arthritis")
> counts<-table(Arthritis$Improved)
#使lab可以旋转
> par(las=2)
> barplot(counts,horiz=TRUE,cex.names=0.8,names.arg=c("No improved","some improved","marked improved"))
>
(2)饼图
> par(mfrow=c(2,2))
> x<-c(10,12,4,16,8)
> lab<-c("US","UK","Australia","Germany","France")
> pie(x,lab,main)
> pie(x,lab,main="Simple Pie Chart")
> pct<-round(x/sum(x)*100)
> pct
[1] 20 24 8 32 16
> labl<-paste(lab,"",pct,"%",sep="")
> labl
[1] "US20%" "UK24%" "Australia8%"
[4] "Germany32%" "France16%"
> pie(x,labl,col=rainbow(length(labl)),main="Pie Chart with Percentage")
> library(plotrix)
> pie3D(x,explode=0.1,main="3D Pie Chart")
#explode越大,那么饼图的间隙就越大
> fan.plot(x,labels=lab,main="Fan plot")
(3)直方图
> hist<-mtcars$mpg
> hist(x)
#在x轴上划分12组数据
hist(x,breaks=12,col="red",xlab="Miles Per Callon")
> x<-mtcars$mpg
> x
[1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4
[9] 22.8 19.2 17.8 16.4 17.3 15.2 10.4 10.4
[17] 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3
[25] 19.2 27.3 26.0 30.4 15.8 19.7 15.0 21.4
#如果freq=FALSE,那么直方图表现的是概率密度,也就是百分比
> hist(x,freq=FALSE,breaks=12,col="green",xlab="Mile Per Callon")
> hist(x,freq=FALSE,breaks=12,col="green",xlab="Mile Per Callon")
> rug(jitter(x))
> lines(density(x),col="red",lwd=2)
(4)plot
> x<-density(mtcars$mpg)
> plot(x)
> mtcars$mpg
[1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4
[9] 22.8 19.2 17.8 16.4 17.3 15.2 10.4 10.4
[17] 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3
[25] 19.2 27.3 26.0 30.4 15.8 19.7 15.0 21.4
> mtcars$cyl
[1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4
[22] 8 8 8 8 4 4 4 8 6 8 4
> sm.density.compare(mtcars$mpg,mtcars$cyl,xlab="Mile per gallon")
>
(5)箱线图
#mpg~cyl表示cyl对mpg的影响
> boxplot(mpg~cyl,data=mtcars,main="Car maileage data",xlab="Number of cylinders",ylab = "Miles per gallon")
若有更加复杂的绘图模型会实时更新,佬们可以实时关注!!💖💖💖