分组绘制点
library(gcookbook)
library(ggplot2)
hw=heightweight
hw$weightGroup=cut(hw$weightLb,breaks=c(-Inf,100,Inf),labels=c("<100",">=100"))
ggplot(hw,aes(x=ageYear,y=heightIn,shape=sex,fill=weightGroup))+
geom_point(size=2.5)+scale_shape_manual(values = c(21,24))+
scale_fill_manual(values=c(NA,"black"),guide=guide_legend(override.aes = list(shape=21)))
cut()函数:cut(vector,cuttime,labels=F) ,vector为被划分的对象,cuttime分割点,label为标注。guide_legend()图例系统。
统计二元变量(散点图栅格化)
当散点图中点过多,密度不好分析时可用该方法直观显示点的分布疏密特征。
sp=ggplot(diamonds,aes(x=carat,y=price))
sp+sta_bin2d()
sp+stat_bin2d(bin=50)+scale_fill_gradient(low="lightblue",high="red",limits=c(0,6000))
library(hexbin)
sp+stat_binhex()+scale_fill_gradient(low="lightblue",high="red",breaks=c(0,1000,2000,4000,6000),limits=c(0,6000))
scale_fill_gradient为设置色带渲染。bin=50即设置栅格系统为50列。stat_binhex()六边形显示。
图表标注数学公式
sp+annotate("text",label="r^2==0.42",parse=TRUE,x=3.2,y=2000)
x,y为标注的横纵坐标,如果要绘制数学公式要设置parse=TRUE,
数学公式一览
更多请看R语言作图:数学公式
标注字体样式
library(MASS)
p=ggplot(faithful,aes(x=eruptions,y=waiting))+geom_point()
p+annotate("text",x=3,y=48,label="Group1",family="serif",fontface="italic",colour="darkred",size=7)+
annotate("text",x=4.5,y=66,label="Group2",family="serif",fontface="bold",colour="darkred",size=20,alpha=0.2)+
annotate("text",x=2.5,y=26,label="Group3",family="serif",fontface=4,colour="blue",size=10)
注:fontface中1=plain,2=bold,3=italic,4=bold-italic
坐标轴标注
p+annotate("text",x=-Inf,y=Inf,label="Upper left",hjust=-.2,vjust=2)+
annotate("text",x=mean(range(faithful$eruptions)),y=-Inf,vjust=-.4,label="Bottom label")
标注线
p=ggplot(heightweight,aes(x=ageYear,y=heightIn,colour=sex))+geom_point()
p+geom_hline(yintercept = 60)+geom_vline(xintercept=14)
p+geom_abline(intercept = 37.5,slope=1.75)
intercept截距,slope斜率
标注箭头
p=ggplot(subset(climate,Source="Berkeley"),aes(x=Year,y=Anomaly10y))+geom_line()
p+annotate("segment",x=1950,xend=1980,y=-0.25,yend=-0.25)
p+annotate("segment",x=1850,xend=1820,y=-0.8,yend=-0.95,colour="blue",size=2,arrow=arrow())+
annotate("segment",x=1950,xend=1980,y=-0.25,yend=-0.25,arrow=arrow(ends="both",angle = 60,length=unit(0.5,"cm")))
ends="both"两头箭头,angle为箭头的角度。
标注长方形
p=ggplot(subset(climate,Source="Berkeley"),aes(x=Year,y=Anomaly10y))+geom_line()
p+annotate("rect",xmin=1950,xmax=1980,ymin=-Inf,ymax=Inf,alpha=.1,fill="blue")
图表标注2
sp=ggplot(subset(countries,Year==2009&healthexp>2000),aes(x=healthexp,y=infmortality))+
geom_point()
sp+geom_text(aes(label=Name),size=4)
sp+geom_text(aes(y=infmortality+0.1,label=Name),size=4,vjust=0)
sp+geom_text(aes(x=healthexp+100,label=Name),size=4,hjust=0)
hjust水平方向移动,vjust垂直方向的移动
密度曲线2
library(MASS)
ggplot(faithful,aes(x=waiting))+geom_density()+expand_limits(x=-1)
ggplot(faithful,aes(x=waiting))+geom_density()+xlim(75,100)
ggplot(faithful,aes(x=waiting))+
geom_line(stat="density",adjust=0.25,colour="red")+
geom_line(stat="density")+
geom_line(stat="density",adjust=2,colour="blue")
adjust调整曲线平滑程度,xlim设置x轴显示范围,expand_limits表示扩展到某一范围
小提琴图
p=ggplot(heightweight,aes(x=sex,y=heightIn))
p+geom_violin()
p+geom_violin(trim=FALSE)
p+geom_violin(adjust=2)
trim=FALSE将两头设置成尖的,adjust调整曲线平滑程度。
箱图2
library(MASS)
ggplot(birthwt,aes(x=factor(race),y=bwt))+geom_boxplot(notch=TRUE)
ggplot(birthwt,aes(x=factor(race),y=bwt))+geom_boxplot()+
stat_summary(fun.y="mean",geom="point",shape=23,size=10,fill="BLUE")
图二标注每组平均值。
连续密度热力图
p=ggplot(faithful,aes(x=eruptions,y=waiting))
p+geom_point()+stat_density2d()
p+stat_density2d(aes(colour=..level..))
p+stat_density2d(aes(fill=..density..),geom="raster",contour=FALSE)
p+stat_density2d(aes(fill=..density..),geom="tile",contour=FALSE)
geom="tile"方格图,方格子比较小。contour=FALSE是否添加等高线。
参考资料:
- gcookbook