当前位置: 首页 > news >正文

梳理系统学习R语言1-R语言实战-使用ggplot进行高阶绘图

以下为书中代码,会添加一些理解

library("ggplot2")
ggplot(data=mtcars,aes(x=wt,y=mpg))+geom_point()+geom_point(pch=17,color="blue",size=2)+geom_smooth(method="lm",color="red",linetype=2)+labs(title="Automobile Data",x="Weight",y="Miles per Gallon")#分面
mtcars$am<-factor(mtcars$am,levels=c(0,1),labels = c("Automatic","Manual"))
mtcars$vs<-factor(mtcars$vs,levels=c(0,1),labels=c("V","S"))
mtcars$cyl<-factor(mtcars$cyl)
ggplot(data=mtcars,aes(x=wt,y=mpg,shape=cyl,color=cyl))+geom_point(size=3)+facet_grid(am~vs)+labs(title="Automobile Data by Engine Type",x="Horsepower",y="Miles per Gallon")data(singer,package="lattice")
ggplot(singer,aes(x=height))+geom_histogram()
ggplot(singer,aes(x=voice.part,y=height))+geom_boxplot()install.packages("carData")
data(Salaries, package="carData")
ggplot(singer,aes(x=voice.part,y=height))+geom_violin(fill="lightblue")+geom_boxplot(fill="lightgreen",width=.2)ggplot(data=Salaries,aes(x=salary,fill=rank))+geom_density(alpha=.3)ggplot(Salaries,aes(x=yrs.since.phd,y=salary,color=rank,shape=sex))+geom_point()ggplot(Salaries,aes(x=rank,fill=sex))+geom_bar(position="stack")+labs(title='position="stack"')ggplot(Salaries,aes(x=rank,fill=sex))+geom_bar(position="dodge")+labs(title='position="dodge"')ggplot(Salaries,aes(x=rank,fill=sex))+geom_bar(position="dodge")+labs(title='position="fill"')
#将y轴改为y="proportion"
ggplot(Salaries,aes(x=rank,fill=sex))+geom_bar(position="dodge")+labs(title='position="fill"',y="proportion")#fill在aes()函数中,应该是变量的名称,在函数外,应该是颜色
ggplot(Salaries,aes(x=rank,fill=sex))+geom_bar()
ggplot(Salaries,aes(x=rank))+geom_bar(fill="red")
ggplot(Salaries,aes(x=rank,fill="red"))+geom_bar()#刻面
ggplot(singer,aes(x=height))+geom_histogram()+facet_wrap(~voice.part,nrow=4)ggplot(Salaries,aes(x=yrs.since.phd,y=salary,color=rank,shape=rank))+geom_point()+facet_grid(.~sex)ggplot(singer,aes(x=height,fill=voice.part))+geom_density()+facet_grid(voice.part~.)#添加光滑曲线
ggplot(Salaries,aes(x=yrs.since.phd,y=salary))+geom_smooth()+geom_point()ggplot(Salaries,aes(x=yrs.since.phd,y=salary,linetype=sex,shape=sex,color=sex))+geom_smooth(method=lm,formula=y~poly(x,2),se=FALSE,size=1)+geom_point(size=2)#修改ggplot()图形外观
ggplot(Salaries,aes(x=rank,y=salary,fill=sex))+geom_boxplot()+scale_x_discrete(breaks=c("AsstProf","AssocProf","Prof"),labels=c("assis","associate","Full"))+scale_y_continuous(breaks=c(50000,100000,150000,200000),labels=c("$50K","$100K","$150K","$200K"))+labs(title='test',x="",y="")
http://www.lryc.cn/news/139422.html

相关文章:

  • 测试框架pytest教程(2)-用例依赖库-pytest-dependency
  • electron软件安装时,默认选择为全部用户安装
  • MySQL常用表级操作
  • Golang Gorm 一对多关系 关系表创建
  • java八股文面试[数据结构]——ConcurrentHashMap原理
  • 学习记录——FeatEnHancer
  • OpenCV中常用的函数
  • 【福利】Google Cloud Next ’23 精彩待发,Cloud Ace 作为联合赞助商提前发福利~
  • vue-admin-template实现按钮级控制
  • 数据驱动工作效率提升的5个层次—以PreMaint设备数字化平台为例
  • 白介素对NK细胞功能的影响(IL-1β、IL-12、IL-15、IL-18、IL-21)
  • C++笔记之虚函数重写规则、返回类型协变、函数的隐藏
  • 抢鲜体验!vLive虚拟直播5大实用新功能上线!
  • 网约车平台如何开发?需要多少钱?
  • Rust踩雷笔记(5)——刷点链表的题(涉及智能指针Box,持续更新)
  • [附源码]计算机毕业设计-JAVA火车票订票管理系统-springboot-论-文-ppt
  • CARLA spawn Actor (Vehicle and Pedestrian)
  • 【官方中文文档】Mybatis-Spring #SqlSessionFactoryBean
  • el-tree树回显删除某项,再次点开树形组件无变化,实际数据已改变
  • 生产作业标准化是什么?生产车间作业流程标准化的步骤
  • CSS3盒模型+flex
  • 物种气候生态位动态量化与分布特征模拟
  • 微服务参数透传实现
  • leetcode 767. Reorganize String(重组字符串)
  • java八股文面试[数据结构]——List和Set的区别
  • 脑机接口里程碑!一天2篇Nature!
  • C语言strchr函数
  • Linux下的Shell基础——Shell概述和入门(一)
  • 当你在浏览器中输入了网址访问时产生了哪些技术步骤
  • 嵌入式Linux人脸检测libfacedetection