日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

昨晚熬夜整理的超級R繪圖技巧——USing ggplot(下)

 geoallan 2021-10-02
# 加載需要的包 library(tidyverse) library(gcookbook)

21.使用facet_grid(x~.)分頁

 ggplot(iris,aes(Sepal.Length))+  geom_histogram(fill='lightblue',colour='red')+  facet_grid(Species~.)
文章圖片1

22.三個(gè)因素相互覆蓋的柱形圖

ggplot(iris,aes(Sepal.Length,fill=Species))+ geom_histogram(bins=30,position = 'identity',alpha=0.5)
文章圖片2

23.柱形圖密度圖結(jié)合

 ggplot(faithful,aes(waiting,..density..))+  geom_histogram(fill='cornsilk',colour='grey60',size=2)+  geom_density()
文章圖片3
# 將x軸線限制在一個(gè)范圍 ggplot(faithful,aes(waiting,..density..))+ geom_histogram(fill='cornsilk',colour='grey60',size=2)+ geom_density()+ xlim(35,105)
文章圖片4

24.頻率多變圖geom_freqpoly

 ggplot(faithful,aes(waiting))+  geom_freqpoly()
文章圖片5
# 進(jìn)一步更改 ggplot(faithful,aes(waiting))+ geom_freqpoly(binwidth=4)
文章圖片6

25.outlier.size設(shè)置箱型圖異常值的大小和形狀

 ggplot(iris,aes(x=factor(Species),y=Sepal.Length))+  geom_boxplot(outlier.size = 5,outlier.shape = 21)
文章圖片7

26.notch增加凹凸口

ggplot(iris,aes(x=factor(Species),y=Sepal.Length))+ geom_boxplot(notch = TRUE)
文章圖片8

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')
文章圖片9

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()
文章圖片10

29.geom_ribbon畫置信區(qū)間

ggplot(climate,aes(Year,Anomaly10y))+  geom_ribbon(aes(ymin=Anomaly10y-Unc10y,          ymax=Anomaly10y+Unc10y))+  geom_line()+  xlim(1800,1850)
文章圖片11

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)
文章圖片12

31.geom_hline等給圖形添加線

ggplot(mtcars,aes(mpg,cyl))+  geom_point()+  geom_hline(yintercept = 5,size=2)+  geom_vline(xintercept = 25,size=2)
文章圖片13

32.geom_abline畫斜線

ggplot(mtcars,aes(mpg,cyl))+ geom_point()+ geom_abline(intercept=1,slope =0.3)
文章圖片14

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())
文章圖片15

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)
文章圖片16

35.直接使用ggtitle添加標(biāo)題

ggplot(climate,aes(Year,Anomaly10y))+  geom_line()+  ggtitle('這里是標(biāo)題')
文章圖片17

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))
文章圖片18

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'))
文章圖片19

38.通過theme修改標(biāo)簽

p38 <- ggplot(cabbage_exp,aes(Date,Weight,fill=Cultivar))+ geom_bar(stat = 'identity',position ='dodge')
文章圖片20
# 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))
文章圖片21
# 去除legend的方法 p38+theme(legend.position = 'none')
文章圖片22
# 修改legend的位置,將其放在頂端 p38+theme(legend.position = 'top')
文章圖片23
#使用坐標(biāo)固定legend的位置 p38+theme(legend.position = c(.85,.2))+ theme(legend.background =element_rect(fill = 'lightblue',colour='black',size=1))
文章圖片24
# 修改legend的名稱 p38+labs(fill='這是legend的名稱')
文章圖片25
# 修改legendlabel的名字 p38+scale_fill_discrete(labels=c('第一','第二'))
文章圖片26

    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多