美文网首页
ggplot2 day4

ggplot2 day4

作者: Silmarillion123 | 来源:发表于2020-06-09 20:08 被阅读0次

ggplot(heightweight,aes(x=ageYear,y=heightIn,shape=sex,color=sex))+geom_point()+scale_shape_manual(values=c(1,2))+scale_color_brewer(palette="Set1")

image.png
heightweight$weightgroup <-cut(heightweight$weightLb,breaks=c(-Inf,100,Inf),labels=c("<100",">100"))#添加新的一列weightgroup,将其根据weightlb的值分为两类 ggplot(heightweight,aes(x=ageYear,y=heightIn,shape=sex,fill=weightgroup))+geom_point()+scale_shape_manual(values = c(24,21))+scale_fill_manual(values=c(NA,"black"),guide=guide_legend(override.aes = list(shape=21,size=3)))#根据weightgroup决定是否填充,图例的设置利用guide_legend函数,使用override.aes函数可以对多种性质进行设置
image.png
图例的相关设置:
theme(legend.title = element_text(size=15, color = "firebrick"), #图例标题的相关设置

当设置为theme(legend.title=element_blank())时,表示删除图例的标题。

      legend.text = element_text(size=10),      ###  图例文本的设置

      legend.position=c(0.95, 0.05),###图例位置,可设置坐标值,也可是'top','bottom','left','right'。默认为right

      legend.key=element_rect(fill='green'))    ###  图例的符号,设置后填充的形状可与绘图对象设置的形状保持一致。file表示填充色。

legend.background=element_rect(colour="purple",fill="pink",size=3,linetype="dashed")

图例形状大小的设置: + guides(colour = guide_legend(override.aes = list(size=5)))

去掉Legend两种方法:1,+NoLegend() 2, +theme(legend.position='none')

ggplot(heightweight,aes(x=ageYear,y=heightIn,size=weightLb))+geom_point()+scale_size_continuous(range =c(0.5,3) )#连续型变量也可映射到颜色或者大小上,其中大小界限可以自行决定

image.png
ggplot(heightweight,aes(x=ageYear,y=heightIn,fill=weightLb))+geom_point(shape=21,size=2.5)+scale_fill_gradient(low="black",high="white")#如果shape选择了有边框线的,可以将连续型变量映射到fill
image.png
ggplot(heightweight,aes(x=ageYear,y=heightIn,fill=weightLb))+geom_point(shape=21,size=2.5)+scale_fill_gradient(low="black",high="white",breaks=seq(70,170,by=20),guide = guide_legend())#use guide_legend() to replace the continuous color bar by discrete color bar.
image.png
ggplot(heightweight,aes(x=ageYear,y=heightIn,size=weightLb,colour=sex))+geom_point(alpha=0.5)+ scale_size_area()+#使数据点和面积成正比 scale_colour_brewer(palette = "Set1")
image.png
ggplot(diamonds,aes(x=carat,y=price))+geom_point(alpha=0.1)#通过调整透明度解决图形重叠的问题
image.png
ggplot(diamonds,aes(x=carat,y=price))+stat_bin2d(bins=50)+scale_fill_gradient(low="lightblue",high="red",limit=c(0,6000))#,使用箱型来解决图形堆积,调用limit函数手动将范围设定到最大值6000
image.png

添加拟合曲线

ggplot(heightweight,aes(x=ageYear,y=heightIn))+geom_point()+stat_smooth(method = lm)#默认情况下会添加95%置信度,可以用level函数手动设置

image.png
ggplot(heightweight,aes(x=ageYear,y=heightIn))+geom_point()+stat_smooth(method = lm,level=0.99)
image.png
ggplot(heightweight,aes(x=ageYear,y=heightIn))+geom_point()+stat_smooth(method = lm,se=FALSE)#无置信度
image.png
ggplot(heightweight,aes(x=ageYear,y=heightIn))+geom_point(colour="grey60")+stat_smooth(method = lm,se=FALSE,colour="black")#看上去清楚一点
image.png
ggplot(heightweight,aes(x=ageYear,y=heightIn))+geom_point()+stat_smooth(method=loess)#默认情况采取loess曲线(局部加权多项式)
image.png

最基础的做法,首先建立模型(根据所需的方法),再使用predict()函数做出预测,得到大量预测点,绘图

model<-lm(heightIn~ageYear+I(ageYear^2),heightweight)#建立模型 model xmin<-min(heightweight$ageYear) xmax<-max(heightweight$ageYear) predicted<-data.frame(ageYear=seq(xmin,xmax,length.out = 100))#在最小值和最大值之间取100个值 predicted$heightIn<-predict(model,predicted) predicted ggplot(heightweight,aes(x=ageYear,y=heightIn))+geom_point(alpha=0.1)+geom_line(data=predicted,size=1)#运用模型绘出线条

image.png

也可使用predictvals()函数

predictvars<-function(model,xvar,yvar,xrange=NULL,sample=100,...){
  if (is.null(xrange)){
    if (any(class(model)%in% c("lm","glm")))
      xrange<-range(model$model[[xvar]])
    else if (any(class(model) %in% "loess"))
      xrange <- range(model$x)
  }
  newdata<-data.frame(x=seq(xrange[1],xrange[2],length.out = sample))
  names(newdata)<-xvar
  newdata[[yvar]]<-predict(model,newdata = newdata,...)
  newdata
}

利用上述公式建立Logistic回归,需要加上type="response",因为默认情况下改函数返回的结果是线型的

model<-glm(classn~V1,b,family = "binomial")
glm_predicted<-predictvars(model,"V1","classn",type="response")                 
ggplot(b,aes(x=V1,y=classn))+geom_point(position=position_jitter(width=0.3,height=0.06),alpha=0.4,shape=21,size=1.5)+geom_line(data=glm_predicted,color="black",size=1)
image.png
make_model<-function(data){
  lm(heightIn~ageYear,data)
}#创建构建模型函数,供后续的dlply函数中使用
models<-dlply(heightweight,"sex", make_model)#dlply函数可将数据框根据某项分割为多部份
models
predvals<- ldply(models,.fun=predictvars,xvar="ageYear",yvar="heightIn")#ldply函数可将分成几部分的数据框一起进行函数处理
predvals 
ggplot(heightweight,aes(x=ageYear,y=heightIn,colour=sex))+geom_point()+geom_line(data=predvals)
image.png
在以上的图中加文字,如模型系数
ggplot(heightweight,aes(x=ageYear,y=heightIn,colour=sex))+geom_point()+geom_line(data=predvals)+annotate("text",label="111",x=16.5,y=52)
image.png
数学公式可以用parse=true来实现
ggplot(heightweight,aes(x=ageYear,y=heightIn,colour=sex))+geom_point()+geom_line(data=predvals)+annotate("text",label="x^2",parse=TRUE,x=16.5,y=52) image.png
添加边际地毯,记录相邻两次的间隔时间
ggplot(heightweight,aes(x=ageYear,y=heightIn,colour=sex))+geom_point()+geom_line(data=predvals)+annotate("text",label="x^2",parse=TRUE,x=16.5,y=52)+geom_rug()
image.png
若添加扰动减少线宽效果更佳
ggplot(faithful,aes(x=eruptions,y=waiting))+geom_point()+geom_rug(position="jitter",size=0.2) image.png

气泡图

cdat<-subset(countries,Year==2009&Name %in% c("Canada","Ireland","United Kindom","United States","New Zealand","Iceland","Japan"))
ggplot(cdat,aes(x=healthexp,y=infmortality,size=GDP))+geom_point(shape=21,colour="black",fill="cornsilk")+scale_size_area(max_size = 10)+xlim(1000,10000)+ylim(0,10)
image.png

相关文章

网友评论

      本文标题:ggplot2 day4

      本文链接:https://www.haomeiwen.com/subject/dtpetktx.html