barplot(height , beside = F, horiz = F)
horiz
为真时绘制水平条形图beside
为F时绘制堆积柱形图heignt
分为三类:beside
为F时每列数据绘制一个堆积柱形beside
为T时每列数据绘制多个并列柱形barplot(1:12,col=rainbow(12),names.arg=paste0(1:12,"月"),
width=1.5, space=1,border=NA,axes=FALSE)
names.arg
表示横坐标名称width
表示柱子宽度space
表示柱子间距border
表示柱子有无边框,可以是逻辑值,可以是颜色向量axes
表示是否显示坐标轴将两个柱形图拼接在一起:
使用 add=TRUE
,同时用xlim控制绘图区域避免重叠
barplot(c(3,5,7),xlim=c(0,10),col=c('#F8B195','#F67280','#C06C84'),width=1,border=F)
barplot(c(4,6,5),xlim=c(0,10),width=1,col=c('#012340','#023059','#4A738C'),add=TRUE,space=c(5,0.2,0.2),border=F)
选择beside=T或者F来堆叠或者并列图形
> fruit
Q1 Q2 Q3 Q4
apple 22 18 16 28
banana 20 17 25 23
orange 29 18 15 26
>barplot(fruit,col=c('#F8B195','#F67280','#C06C84'),
names.arg=paste0(1:4,"季度"),
legend.text=c("苹果","香蕉","橙子"),
args.legend = list(x='top',ncol=3),
ylim = c(0,37),
main="水果全年销量",
col.main="black",
cex.main=1.7,
border = F,
beside=TRUE)
y~x1+x2
代替,y是因变量,x1x2顺序会影响堆积方式citydata $population, names.arg=citydata $city,
> stucount
major sex count
1 经济 男 15
2 经济 女 25
3 统计 男 22
4 统计 女 13
5 管理 男 20
6 管理 女 12
> barplot(count~major+sex,data=stucount,
col=c('#F8B195','#F67280','#C06C84'),legend.text=TRUE,
main="男女生人数对比",
ylim=c(0,60),xlim=c(0,10),width=3,
xlab="性别",ylab="人数")
四种表现形式
boxplot(v1,v2,v3) 每个向量都是一个箱体
boxplot(list(tag1=v1,tag2=v2,...)每个组件是一个箱体
boxplot(matrix)
boxplot(y~x)数值型向量y用因子x分组
economics <- pmin(round(rnorm(120,mean=78,sd=8.5)),100)
statistics <- pmin(round(rnorm(70,mean=83,sd=12.3)),100)
management <- pmin(round(rnorm(80,mean=73,sd=5.2)),100)
boxplot(economics,statistics,management,
names=c("经济学","统计学","管理学"),
boxwex=0.4,
col=c('#F8B195','#F67280','#C06C84'),
main="不同专业成绩对比",
cex.main=1.2
)
> height
[1] 172 166 175 162 166 178 185 159 172 170 160 180
> sex
[1] "男" "女" "男" "女" "女" "男" "男" "女" "女" "男" "女" "男"
> boxplot(height~sex,
col=c('#F67280','#C06C84'),
boxwex=0.4)
boxplot(score~major,data=s,
col=c('#F8B195','#F67280','#C06C84'),boxwex=0.4)
add=TRUE
在已有图形上添加箱体图subset
给出要使用哪些行at
给出绘制箱体的位置,默认是1:n,-号表示向左偏移,+表示向右偏移attach(ToothGrowth)
boxplot(len ~ dose, at = 1:3-0.2,
subset = (supp == "VC"),
boxwex = 0.25, col = "yellow",
main = "Guinea Pigs' Tooth Growth",
xlab = "Vitamin C dose mg",
ylab = "tooth length", ylim = c(0, 35))
boxplot(len ~ dose, add = TRUE, at = 1:3+0.2,
subset = supp == "OJ",
col = "orange",boxwex = 0.25)
legend(2, 9, c("Ascorbic acid", "Orange juice"),
fill = c("yellow", "orange"))
hist(x, breaks, right, labels)
x
:要被分段的数值型向量breaks
:分段点,有3种形式right
:是否右闭,默认TRUElabels
:每个柱形上面添加的标签hist(score,breaks=c(50,60,70,80,90,100),
col=c('#658791','#F2EBD5','#F2A62A','#F27649','#B4766A'),
include.lowest=TRUE,right=FALSE,border = F)
所有矩形的总面积为1
ret<-hist(score,breaks=c(50,60,70,85,100),
include.lowest=TRUE,right=FALSE,
col=c('#658791','#F2EBD5','#F2A62A','#F27649'),
ylim=c(0,0.05),border=F,
labels = c("差(看面积)","中(看面积)","良(看面积)","优(看面积)"),
main="成绩分布")
pie(x, labels = names(x), clockwise = F, col,
radius = 0.8, init.angle = 0)
x
:数值型向量labels
:用于切片的描述clockwise
:是否顺时针绘制,默认假col
:颜色radius
:饼图的半径init.angle
:初始的角度pie(c(3,5,7,4),labels=c("苹果","香蕉","橙子","葡萄"),
clockwise = TRUE, radius = 1,
col=c('#658791','#F2EBD5','#F2A62A','#F27649'),
init.angle=45,border = F)
需要安装包plotrix
,引用其中的pie3D函数
为了描述更多的参数,我们绘制下面这个不太好看的饼图:
ret<-pie3D(c(3,5,7,4),labels=c("苹果","香蕉","橙子","葡萄"),
height = 0.2, edges = 4, theta = pi/3, shade = 0.6,
radius=1.2, explode = 0.2,
col=c('#658791','#F2EBD5','#F2A62A','#F27649'),border = F)
edges
:形成圆的线数height
:饼图的高度theta
:看饼图的视角start
:开始绘制扇区的角度labels
:每个扇形的可选标签shade
:0-1之间的小数,用来衡量阴影的大小,数字越大阴影越小plot(x, y, main, xlab, ylab, xlim, ylim)
main
:主标题xlab ylab
:坐标轴标签xlim ylim
:图像上x、y的取值范围plot(x=mtcars$wt,y=mtcars$mpg,
xlab="重量(千磅)",ylab="单位加仑汽油行驶英里数")
pairs(formula, data)
formula
表示成对使用的一系列变量,变量之间两两配对画出散点图data
是获取的数据框pairs(~wt+mpg+disp,data=mtcars,col = '#FF5964',
main = "散点图矩阵")
plot(x, y, type, pch)
#半个月每日消耗的电量情况(千瓦时)
x<-1:20
#张三家日耗电量
y<-rnorm(20,mean=4,sd=0.7)
#李四家日耗电量
z <- rnorm(20,mean=6,sd=1)
#ylim给出y轴数值范围,为之后的绘制预留空间
plot(x,y,type="b",col="#3A7D7C",
xlab="日期",ylab="日耗电量(千瓦时)",
ylim=c(0,10),pch=16)
#使用lines函数在已有图形上增加折线图
lines(x,z,col="#EC5A31",type="b",pch=15)
#添加图例
legend(x=2,y=10,
legend=c("张三家","李四家"),
pch=c(15,2),col=c("#3A7D7C","#EC5A31"))