No.13 数据可视化02
主要内容:
- 条形图
- 饼图
- 直方图
- 核密度图等
1. 条形图 barplot()
1.1 调用数据mtcars
1 2 | 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( )函数
1 2 3 4 5 6 | #统计频数 table (mtcars$gear)结果:> #统计频数 > table (mtcars$gear) 3 4 5 15 12 5 |
1 2 3 | barplot ( table (mtcars$gear), names.arg = c ( 'gear-3' , 'gear-4' , 'gear-5' ), ylim = c (0,20), col= c ( "blue" , "green" , "red" )) |
1 2 3 4 | barplot ( table (mtcars$gear), names.arg = c ( 'gear-3' , 'gear-4' , 'gear-5' ), <strong> horiz = T, </strong> #控制横向还是纵向 xlim = c (0,20), col= c ( "blue" , "green" , "red" )) |
1.3 叠加柱状图 (矩阵输入)
1 2 3 4 5 6 7 8 | 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 |
1 2 3 4 5 6 7 8 9 10 | 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 |
1 2 3 | 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( )
1 2 3 | #R自带调色板 a <- rainbow (5) pie ( c (1,1,1,1,1),col=a) |
3. 直方图 hist( )
1 | hist (mtcars$disp) |
4. 其他
lines() #在plot() 绘制的图像上再绘制图像(曲线)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了