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

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

bash 复制代码
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="")

#图例
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="",fill="Gender")+theme(legend.position = c(.1,.8))

ggplot(mtcars,aes(x=wt,y=mpg,size=disp))+geom_point(shape=21,color="black",fill="cornsilk")+
  labs(x="Weight",y="Miles Per Gallon",
       title="Bubble Chart",size="Engize\nDisplacement")


ggplot(Salaries,aes(x=yrs.since.phd,y=salary,color=rank))+scale_color_manual(values=c("orange","olivedrab","navy"))+geom_point(size=2)




mytheme<-theme(plot.title = element_text(face="bold.italic",size="14",color="brown"),
               axis.title=element_text(face="bold.italic",size = 10,color = "brown"),
               axis.text=element_text(face="bold",size=9,
                                      color="darkblue"),
               panel.background = element_rect(fill="white",
                                               color="darkblue"),
               panel.grid.major.y = element_line(color="gray",
                                                 linetype=1),
               panel.grid.minor.y=element_line(color="grey",
                                               linetype=2),
               panel.grid.minor.x=element_blank(),
               legend.position = "top")

ggplot(Salaries,aes(x=rank,y=salary,fill=sex))+
  geom_boxplot()+labs(title = "Salary by Rank and sex",x="Rank",y="Salary")+mytheme
相关推荐
love_and_hope2 分钟前
Pytorch学习--神经网络--搭建小实战(手撕CIFAR 10 model structure)和 Sequential 的使用
人工智能·pytorch·python·深度学习·学习
Chef_Chen6 分钟前
从0开始学习机器学习--Day14--如何优化神经网络的代价函数
神经网络·学习·机器学习
芊寻(嵌入式)15 分钟前
C转C++学习笔记--基础知识摘录总结
开发语言·c++·笔记·学习
hong1616881 小时前
跨模态对齐与跨领域学习
学习
阿伟来咯~2 小时前
记录学习react的一些内容
javascript·学习·react.js
Suckerbin2 小时前
Hms?: 1渗透测试
学习·安全·网络安全
水豚AI课代表2 小时前
分析报告、调研报告、工作方案等的提示词
大数据·人工智能·学习·chatgpt·aigc
聪明的墨菲特i2 小时前
Python爬虫学习
爬虫·python·学习
Diamond技术流2 小时前
从0开始学习Linux——网络配置
linux·运维·网络·学习·安全·centos
斑布斑布2 小时前
【linux学习2】linux基本命令行操作总结
linux·运维·服务器·学习