R语言图形绘制

(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")

若有更加复杂的绘图模型会实时更新,佬们可以实时关注!!💖💖💖

相关推荐
吾爱星辰2 小时前
Kotlin 处理字符串和正则表达式(二十一)
java·开发语言·jvm·正则表达式·kotlin
ChinaDragonDreamer2 小时前
Kotlin:2.0.20 的新特性
android·开发语言·kotlin
FreakStudio2 小时前
全网最适合入门的面向对象编程教程:56 Python字符串与序列化-正则表达式和re模块应用
python·单片机·嵌入式·面向对象·电子diy
IT良2 小时前
c#增删改查 (数据操作的基础)
开发语言·c#
丶21362 小时前
【CUDA】【PyTorch】安装 PyTorch 与 CUDA 11.7 的详细步骤
人工智能·pytorch·python
Kalika0-03 小时前
猴子吃桃-C语言
c语言·开发语言·数据结构·算法
_.Switch3 小时前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j
代码雕刻家3 小时前
课设实验-数据结构-单链表-文教文化用品品牌
c语言·开发语言·数据结构
一个闪现必杀技3 小时前
Python入门--函数
开发语言·python·青少年编程·pycharm
Fan_web3 小时前
jQuery——事件委托
开发语言·前端·javascript·css·jquery