目录
条形图
使用ggplot(...)_geom_*(...)格式,具体如下。
#使用ggplot函数初始化图形对象
ggplot(data=NULL,mapping=aes())
#绘制条形图
geom_bar(mapping=NULL, data=NULL, stat="count",
position="stack",..., width=NULL, binwidth=NULL,
na.rm=FALSE, show.legend=NA, inherit.aes=TRUE)
饼图与环形图
基于条形图的极坐标变换可以得到饼图。
# 绘制饼图或环形图
# 构造绘图数据
ratio = c(0.2515,0.3724,0.3336,0.0368,0.0057)
edu = c('中专','大专','本科','硕士','其他')
credit <- data.frame(ratio, edu)
credit$edu <- factor(credit$edu, levels = c('中专','大专','其他','本科','硕士'))
# 构造数值标签
labels <- paste0(round(credit$ratio*100,1),'%')
# 基于条形图绘制饼图
ggplot(data = credit, mapping = aes(x = '', y = ratio, fill = edu)) +
geom_bar(stat = 'identity', width = 1) + # 绘制饼图必须保证条形图的宽度大于等于1
# 添加数值标签
geom_text(mapping = aes(x = '', y = ratio, label = labels),
# 调整数值标签的位置(垂直居中)
position = position_stack(vjust = 0.5)) +
coord_polar(theta = 'y') + # 对条形图作极坐标变换
theme(axis.ticks = element_blank(), # 去除图中的刻度线
axis.title = element_blank(), # 去除图中坐标轴的标签
axis.text = element_blank(), # 去除图中坐标轴的刻度标签
panel.grid = element_blank(),# 去除图中的网格线
panel.background = element_blank() # 去除图中的灰色背景
)
矩形图与瓦片图
#使用ggplot函数初始化图形对象
ggplot(data=NULL,mapping=aes())
#绘制矩形图
geom_brect(mapping=NULL, data=NULL, stat="identity",
position="identity",...,
na.rm=FALSE, show.legend=NA, inherit.aes=TRUE)
#绘制瓦片
geom_tile(mapping=NULL, data=NULL, stat="identity",
position="identity",...,
na.rm=FALSE, show.legend=NA, inherit.aes=TRUE)
直方图与频次多边形图
这两种图形的绘制可以借助于geom_histogram函数和geom_freqpoly函数。
#使用ggplot函数初始化图形对象
ggplot(data=NULL,mapping=aes())
#绘制直方图
geom_histogram(mapping=NULL, data=NULL, stat="bin",
bins=NULL,position="stack",...,
binwidth=NULL,na.rm=FALSE, show.legend=NA, inherit.aes=TRUE)
#绘制频次多边形图
geom_tile(mapping=NULL, data=NULL, stat="bin",
bins=NULL, position="stack",...,
binwidth=NULL,na.rm=FALSE, show.legend=NA, inherit.aes=TRUE)
箱线图与小提琴
箱线图主要有两个功能,一个是便于呈现数据的分布特征,二是监督数据中的异常点。小提琴图则形象地刻画了数据的核密度分布。
#使用ggplot函数初始化图形对象
ggplot(data=NULL,mapping=aes())
#绘制箱线图
geom_boxplot(mapping=NULL, data=NULL, stat="boxplot",
position="dodge2", ..., outlier.colour=NULL,
outlier.color=NULL, outlier.fill=NULL, outlier.shape=19,
outlier.size=1.5, outlier.alpha=NULL,coef=1.5,
notch=FALSE, notchwidth=0.5, varwidth=FALSE, width,
na.rm=FALSE, show.legend=NA, inherit.aes=TRUE)
#outlier.colour 与 outlier.color功能一致
#coef 为上下须的范围,默认为1.5倍的四分位差
#使用ggplot函数初始化图形对象
ggplot(data=NULL,mapping=aes())
#绘制小提琴图
geom_violin(mapping=NULL, data=NULL, stat="ydensity",
position="dodge", ..., trim=TRUE, scale="area,
na.rm=FALSE, show.legend=NA, inherit.aes=TRUE)
#trim 表示是否剔除小提琴两端的数据
折线图与阶梯图
这两种图形的绘制需要使用geom_line函数和geom_step函数。
#使用ggplot函数初始化图形对象
ggplot(data=NULL,mapping=aes())
#绘制折线图
geom_line(mapping=NULL, data=NULL, stat="identity",
position="identity",na.rm=FALSE, show.legend=NA,
arrow, inherit.aes=TRUE)
#arrow函数添加箭头
arrow(angle=30,length=unit(0.25,"inches),
end="last",type="open")
# angele指定夹角度数,ends 默认为last, 另外可以设置first以及 both;type 默认为 open 可以设置为closed
#绘制阶梯图
geom_step(mapping=NULL, data=NULL, stat="identity",
position="identity", direction="hv",
na.rm=FALSE, show.legend=NA, inherit.aes=TRUE,...)
面积图与带状图
#使用ggplot函数初始化图形对象
ggplot(data=NULL,mapping=aes())
#绘制面积图
geom_area(mapping=NULL, data=NULL, stat="identity",
position="stack",na.rm=FALSE, show.legend=NA,
inherit.aes=TRUE,...)
#绘制带状图
geom_ribbon(mapping=NULL, data=NULL, stat="identity",
position="identity", ...,
na.rm=FALSE, show.legend=NA, inherit.aes=TRUE,...)
#mapping传入上下限: mapping=aex(x=speed,ymin=lwe, ymax=upr)
散点图与气泡图
geom_point绘制散点图,geom_smooth添加拟合线,
#使用ggplot函数初始化图形对象
ggplot(data=NULL,mapping=aes())
#绘制散点图
geom_point(mapping=NULL, data=NULL, stat="identity",
position="identity",...,na.rm=FALSE, show.legend=NA,
inherit.aes=TRUE,...)
#绘制拟合图
geom_smooth(mapping=NULL, data=NULL, stat="smooth",
position="identity", ...,method="auto",formula=y~x,
se=TRUE,
na.rm=FALSE, show.legend=NA, inherit.aes=TRUE,
inherit.ars=TRUE,span=0.75,level=0.95)
#method lm线性回归 glm广义线性回归, loess 局部加权回归以及MASS包中其他拟合函数
#span 指的是局部加权回归线的光滑程度 0~1
#level 置信区间
#气泡图即在散点图基础上使用+scale_radius函数与scale_size函数
scale_radius(name=waiver(),breaks=waiver(), labels=waiver(),limits=NULL,range=c(1,6),
trans="identity",guide="legend)
scale_size(name=waver().breaks=waiver(),labels=waiver(),limits=NULL,range=c(1,6),
trans="identity",guide="legend")
# range 通过二值向量指定气泡的最小半径和最大半径
区块频次图的绘制
使用geom_bin2d和geom_hex函数绘制矩形块和六边形块,每个区块存放数量不同的样本点。
#使用ggplot函数初始化图形对象
ggplot(data=NULL,mapping=aes())
geom_bin2d(mapping=NULL, data=NULL, stat="bin2d",
position="identity",...,na.rm=FALSE,
bins=c(30,30), binwidth=NULL,drop=TRUE,
show.legend=NA, inherit.aes=TRUE)
geom_hex(mapping=NULL, data=NULL, stat="binhex",
position="identity",...,na.rm=FALSE,
bins=c(30,30), binwidth=NULL,
show.legend=NA, inherit.aes=TRUE)
核密度图
ggplot中可以绘制一维和二维的核密度图。
#使用ggplot函数初始化图形对象
ggplot(data=NULL,mapping=aes())
geom_density(mapping=NULL, data=NULL, stat="density",
position="identity",...,na.rm=FALSE,
show.legend=NA, inherit.aes=TRUE)
geom_density_2d(mapping=NULL, data=NULL, stat="density2d",
position="identity",...,na.rm=FALSE,
contour=TRUE, n=100,h=NULL,
show.legend=NA, inherit.aes=TRUE)
QQ图
#使用ggplot函数初始化图形对象
ggplot(data=NULL,mapping=aes())
geom_qq(mapping=NULL, data=NULL, geom="point"
position="identity",...,distribution=stats:qnorm,
dparams=list(),na.rm=FALSE,
show.legend=NA, inherit.aes=TRUE)
篇章总结