# 加载需要的包
library(tidyverse)
library(gcookbook)
21.使用facet_grid(x~.)分页
ggplot(iris,aes(Sepal.Length))+ geom_histogram(fill="lightblue",colour="red")+ facet_grid(Species~.)
22.三个因素相互覆盖的柱形图
ggplot(iris,aes(Sepal.Length,fill=Species))+ geom_histogram(bins=30,position = "identity",alpha=0.5)
23.柱形图密度图结合
ggplot(faithful,aes(waiting,..density..))+ geom_histogram(fill="cornsilk",colour="grey60",size=2)+ geom_density()
# 将x轴线限制在一个范围
ggplot(faithful,aes(waiting,..density..))+ geom_histogram(fill="cornsilk",colour="grey60",size=2)+ geom_density()+ xlim(35,105)
24.频率多变图geom_freqpoly
ggplot(faithful,aes(waiting))+ geom_freqpoly()
# 进一步更改
ggplot(faithful,aes(waiting))+ geom_freqpoly(binwidth=4)
25.outlier.size设置箱型图异常值的大小和形状
ggplot(iris,aes(x=factor(Species),y=Sepal.Length))+ geom_boxplot(outlier.size = 5,outlier.shape = 21)
26.notch增加凹凸口
ggplot(iris,aes(x=factor(Species),y=Sepal.Length))+ geom_boxplot(notch = TRUE)
27.点线柱子结合,做个点线柱子图试试
ggplot(BOD,aes(Time,demand))+ geom_bar(stat = "identity",fill="lightblue")+ geom_line(size=2,colour="red")+ geom_point(size=5,colour="blue")
28.scale_y_log10()对y轴进行log化,使得y轴的数据相差减小
ggplot(worldpop,aes(Year,Population))+ geom_line()+ geom_point()+ scale_y_log10()
29.geom_ribbon画置信区间
ggplot(climate,aes(Year,Anomaly10y))+ geom_ribbon(aes(ymin=Anomaly10y-Unc10y, ymax=Anomaly10y+Unc10y))+ geom_line()+ xlim(1800,1850)
30.采用虚线画置信区间
ggplot(climate,aes(Year,Anomaly10y))+ geom_line(aes(y=Anomaly10y-Unc10y),colour="grey50",linetype="dotted")+ geom_line(aes(y=Anomaly10y+Unc10y),colour="grey50",linetype="dotted")+ geom_line()+ xlim(1800,1850)
31.geom_hline等给图形添加线
ggplot(mtcars,aes(mpg,cyl))+ geom_point()+ geom_hline(yintercept = 5,size=2)+ geom_vline(xintercept = 25,size=2)
32.geom_abline画斜线
ggplot(mtcars,aes(mpg,cyl))+ geom_point()+ geom_abline(intercept=1,slope =0.3)
33.通过annotate函数给图中增加箭头
ggplot(climate,aes(Year,Anomaly10y))+ geom_line()+ annotate("segment",x=1850,xend = 1900, y=0,yend=-0.1,colour="blue", size=2,arrow=arrow())
34.使用annotate的rect给图形添加阴影
ggplot(climate,aes(Year,Anomaly10y))+ geom_line()+ annotate("rect",xmin = 1850,xmax = 1900, ymin=-0.75,ymax = 0,fill="blue",alpha=.3)
35.直接使用ggtitle添加标题
ggplot(climate,aes(Year,Anomaly10y))+ geom_line()+ ggtitle("这里是标题")
36.Theme设置图形的网格线
# panel.grid.major = element_line()修改网格主线
# panel.grid.minor = element_line()修改网格次线
# panel.background = element_rect()修改网格背景颜色
#panel.border = element_rect()修改网格边线 # element_line()适合修改线
# element_rect()适合修改矩形
ggplot(climate,aes(Year,Anomaly10y))+ geom_line()+ theme(panel.grid.major = element_line(colour="red"), panel.grid.minor = element_line(colour="red",linetype = "dashed",size=0.2), panel.background = element_rect(fill="lightblue"), panel.border = element_rect(colour = "blue",fill = NA,size=2))
37.Theme修改每个标题
# axis.title.x = element_text()修改x轴的标题
# axis.text.x = element_text()修改x轴
# plot.title = element_text()修改ggtitle的字体格式
ggplot(climate,aes(Year,Anomaly10y))+ geom_line()+ ggtitle("这里是标题")+ theme( axis.title.x = element_text(colour = "red",size=24), axis.text.x = element_text(colour = "blue",size=22), axis.title.y=element_text(colour="red",size=24), axis.text.y=element_text(colour = "blue",size=22), plot.title = element_text(colour="red",size=20,face = "bold"))
38.通过theme修改标签
p38 <- ggplot(cabbage_exp,aes(Date,Weight,fill=Cultivar))+ geom_bar(stat = "identity",position ="dodge")
# legend.background = element_rect()修改legend的背景
# legend.title = element_text()修改legend的标题
# legend.text = element_text()修改legend字体颜色
# legend.key=element_rect()修改legend的外边框
p38+theme( legend.background = element_rect(fill="grey85",colour = "red",size=1), legend.title = element_text(colour="blue",face = "bold",size=14), legend.text = element_text(colour = "red",size=18), legend.key=element_rect(colour = "red",size=0.25))
# 去除legend的方法
p38+theme(legend.position = "none")
# 修改legend的位置,将其放在顶端
p38+theme(legend.position = "top")
#使用坐标固定legend的位置
p38+theme(legend.position = c(.85,.2))+ theme(legend.background =element_rect(fill = "lightblue",colour="black",size=1))
# 修改legend的名称
p38+labs(fill="这是legend的名称")
# 修改legend的label的名字
p38+scale_fill_discrete(labels=c("第一","第二"))