R语言学习(六)ggplot2包(三)

分组绘制点
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是否添加等高线。
在这里插入图片描述

参考资料:

  1. gcookbook
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值