主要内容:
- 条形图
- 饼图
- 直方图
- 核密度图等
1. 条形图 barplot()
1.1 调用数据mtcars
mtcars$gear
结果:
> mtcars$gear [1] 4 4 4 3 3 3 3 4 4 4 4 3 3 3 3 3 3 4 4 4 3 3 3 3 3 4 5 5 5 5 5 4
1.2 频数统计 table( )函数
#统计频数 table(mtcars$gear)
结果:
> #统计频数 > table(mtcars$gear) 3 4 5 15 12 5
barplot(table(mtcars$gear), names.arg = c('gear-3','gear-4','gear-5'), ylim = c(0,20), col=c("blue","green","red"))
barplot(table(mtcars$gear), names.arg = c('gear-3','gear-4','gear-5'), horiz = T, #控制横向还是纵向 xlim = c(0,20), col=c("blue","green","red"))
1.3 叠加柱状图 (矩阵输入)
table(mtcars$gear,mtcars$vs) barplot(table(mtcars$gear,mtcars$vs))
结果:
> table(mtcars$gear,mtcars$vs) 0 1 3 12 3 4 2 10 5 4 1
table(mtcars$gear,mtcars$vs) barplot(table(mtcars$gear,mtcars$vs), col=c("blue","green","red")) legend("topright", title = "gear", legend = c(3,4,5), col = c("blue","green","red"), pch = 15, cex = 0.6 ) #pch = 15表示方框, cex = 0.6 缩放因子
结果:
> table(mtcars$gear,mtcars$vs) 0 1 3 12 3 4 2 10 5 4 1
barplot(table(mtcars$gear,mtcars$vs), beside=T, col=c("blue","green","red")) #beside = T表示不堆叠,默认堆叠 legend("topright", title = "gear", legend = c(3,4,5), col = c("blue","green","red"), pch = 15, cex = 0.6 )
2. 饼图 pie( )
#R自带调色板 a <- rainbow(5) pie(c(1,1,1,1,1),col=a)
3. 直方图 hist( )
hist(mtcars$disp)
4. 其他
lines() #在plot() 绘制的图像上再绘制图像(曲线)
标签:No.13,02,gear,barplot,mtcars,vs,可视化,table,col From: https://www.cnblogs.com/bltstop/p/18687482