# 加載需要的包 library(tidyverse) library(gcookbook)
21.使用facet_grid(x~.)分頁 ggplot(iris,aes(Sepal.Length))+ geom_histogram(fill='lightblue',colour='red')+ facet_grid(Species~.)
22.三個(gè)因素相互覆蓋的柱形圖 ggplot(iris,aes(Sepal.Length,fill=Species))+ geom_histogram(bins=30,position = 'identity',alpha=0.5)
23.柱形圖密度圖結(jié)合 ggplot(faithful,aes(waiting,..density..))+ geom_histogram(fill='cornsilk',colour='grey60',size=2)+ geom_density()
# 將x軸線限制在一個(gè)范圍 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()
# 進(jìn)一步更改 ggplot(faithful,aes(waiting))+ geom_freqpoly(binwidth=4)
25.outlier.size設(shè)置箱型圖異常值的大小和形狀 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.點(diǎn)線柱子結(jié)合,做個(gè)點(diǎn)線柱子圖試試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軸進(jìn)行l(wèi)og化,使得y軸的數(shù)據(jù)相差減小ggplot(worldpop,aes(Year,Population))+ geom_line()+ geom_point()+ scale_y_log10()
29.geom_ribbon畫置信區(qū)間ggplot(climate,aes(Year,Anomaly10y))+ geom_ribbon(aes(ymin=Anomaly10y-Unc10y, ymax=Anomaly10y+Unc10y))+ geom_line()+ xlim(1800,1850)
30.采用虛線畫置信區(qū)間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函數(shù)給圖中增加箭頭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添加標(biāo)題ggplot(climate,aes(Year,Anomaly10y))+ geom_line()+ ggtitle('這里是標(biāo)題')
36.Theme設(shè)置圖形的網(wǎng)格線# panel.grid.major = element_line()修改網(wǎng)格主線 # panel.grid.minor = element_line()修改網(wǎng)格次線 # panel.background = element_rect()修改網(wǎng)格背景顏色 #panel.border = element_rect()修改網(wǎng)格邊線 # 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修改每個(gè)標(biāo)題 # axis.title.x = element_text()修改x軸的標(biāo)題 # axis.text.x = element_text()修改x軸 # plot.title = element_text()修改ggtitle的字體格式 ggplot(climate,aes(Year,Anomaly10y))+ geom_line()+ ggtitle('這里是標(biāo)題')+ 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修改標(biāo)簽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的標(biāo)題 # 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')
#使用坐標(biāo)固定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('第一','第二'))
|