前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >r语言ggplot2包学习笔记(上代码+注释)

r语言ggplot2包学习笔记(上代码+注释)

作者头像
学到老
发布2018-03-16 13:32:01
2.1K0
发布2018-03-16 13:32:01
举报
文章被收录于专栏:深度学习之tensorflow实战篇

#ggplot2学习笔记##第一节:尝试ggplot library(ggplot2) #使用的是R内置数据(mpg) qplot(displ,hwy,data=mpg,colour=factor(cyl)) #displ排量x轴,hwy高速油耗y轴,数据源mpg,气缸数cly1. qplot(displ,hwy,data=mpg,facets=.~year)+geom_smooth() #facets分组参数,这里是根据时间分组。geom_smooth()函数为拟合曲线 p <- ggplot(data = diamonds,aes(carat,price,colour=cut)) #aes映射参数 carat映射到X,price到Y,cut到colour summary(p)#查看映射关系 #要有图像一定需要图层,下面我们来建设图层 p <- ggplot(diamonds,aes(x=carat)) p <- p+layer(geom="bar",geom_params=list(fill="steelblue"),stat ="bin",stat_params=list(binwidth=2)) #geom参数:point 基础散点 point(size) 气泡图 bar 条形图 boxplot 箱线图 line直线图 #geom.params设置geom参数这里颜色为铁蓝色。stat指定统计变化,bin为频数。stat.params=list(binwidth=2)统计区间为2 summary(p);p ##layer层简写 可以直接用geom_point()等函数,如下 p <- ggplot(data = diamonds,aes(carat,price,colour=cut))+geom_point() #######总结:ggplot第一步先定义数据映射,第二步定义图层。(就像是PS上画图一样。) ##第二节:ggplot参数说明 #使用mtcars数据 rm( list = ls ( all = TRUE))#清除内存数据 p <- ggplot(mtcars,aes(mpg,wt,colour="cyl"))+geom_point() #修改数据源 mtcars<-transform(mtcars,mpg=mpg^2)#修改数据内容transform()函数 p%+% mtcars#用新的数据暂时取代P里面的数据 #修改参数 p+geom_point(aes(colour=factor(cyl))) summary(p) #作业题百分比排列方式使用position #题目一:堆叠型bar x <- ggplot(diamonds,aes(clarity,fill=cut))#产生对象 x为clarity y没写,则为count,fill填充颜色为cut x + geom_bar(position="stack") +scale_y_continuous(breaks=seq(0,12000,2000)) #堆叠型bar + y刻度从0到12000,单位2000 #题目二:百分比bar x <- ggplot(diamonds,aes(clarity,fill=cut))+geom_bar(position = "fill") #题目三:间隔型变量 x <- ggplot(diamonds,aes(clarity,fill=cut))+geom_bar(position = "dodge") #组合geoms和stats d<- ggplot(diamonds,aes(carat))+xlim(0,3) d + stat_bin(aes(ymax=..count..),binwidth=0.1,geom="area") summary(d) #整合回归画图, 使用nlme 包和Oxboys 数据包 library(nlme) model<- lme(height~age,data = Oxboys,random=~1+age|Subject) #混合线性回归应变量身高,自变量年龄 oplot<- ggplot(Oxboys,aes(age,height,group=Subject))+geom_line() age_grid<- seq(-1,1,length=10) subjects<- unique(Oxboys$Subject) preds<- expand.grid(age = age_grid,Subject=subjects)#将年龄和subject进行排列组合 preds$height<-predict(model,preds)#通过混合线性模型预测排列组合的身高 oplot+geom_line(data=preds,colour="#3366FF",size=0.4)#在原来黑色图层上增加蓝色预测数据 #统计误差图 Oxboys$fitted<- predict(model) Oxboys$resid<- with(Oxboys,fitted-height) oplot %+% Oxboys +aes(y=resid)+geom_smooth(aes(group=1))#Oxboys数据暂时替代oplot再做图 ########### ##第三节:ggplot功能展示 rm( list = ls ( all = TRUE))#清除内存数据 library(ggplot2) df<- data.frame( x=c(3,1,5), y=c(2,4,6), label=c("a","b","c") ) p<- ggplot(df,aes(x,y,label=label))+xlab(NULL)+ylab(NULL) p+geom_point()+ggtitle("geom_point")#散点效果+title p+geom_bar(stat="identity")+ggtitle("geom_bar(stat=\"identity\")") #柱形图。。。注意"需要加\才能引用 p+geom_line()+ggtitle("geom_line")#线图 p+geom_area()+ggtitle("geom_area")#填黑线图 p+geom_path()+ggtitle("geom_path")#路径图 p+geom_tile()+ggtitle("geom_tile")#瓦片图 p+geom_polygon()+ggtitle("geom_polygon")#多边形填充图 #画分布的技巧(1)画密度 depth_dist<- ggplot(diamonds,aes(depth))+xlim(58,68) depth_dist+geom_histogram(aes(y=..density..),binwidth =0.1)+facet_grid(cut~.) #density位统计量”密度“所以前后增加..binwidth取样区间0.1做取样 以cut做分组 #画分布的技巧(2)画分布 depth_dist+geom_histogram(aes(fill=cut),binwidth=0.1,position="fill") #overplotting处理(几何对象重叠处理) rm( list = ls ( all = TRUE))#清除内存数据 df<- data.frame(x=rnorm(2000),y=rnorm(2000)) norm<-ggplot(df,aes(x,y)) norm+geom_point()#我们会发现很多点都重合了,那就是重叠数据 norm+geom_point(shape=1)#改变shape达到目的 norm+geom_point(shape=".")#改变点的类型 norm+geom_point(alpha=1/3)#使用透明度参数 #案例2 diamonds数据库 td<-ggplot(diamonds,aes(table,depth))+xlim(50,70)+ylim(50,70) td+geom_point() td+geom_jitter(position = position_jitter(width=0.5))#使用扰动解决重叠数据

td+geom_jitter(position = position_jitter(width=0.5),alpha=1/10)

参考文献:

《R语言应用系列丛书·ggplot2:数据分析与图形艺术》

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档