06 R語言作圖
圖就是數(shù)據(jù),數(shù)據(jù)就是圖
常用可視化R包
作圖:base,ggplot2, ggpubr;
拼圖:patchwork;
導(dǎo)出:pdf()等三段論, ggsave, eoffice-topptx。

image.png
低級(jí)繪圖參數(shù)作用是在高級(jí)繪圖函數(shù)添磚加瓦,而繪圖參數(shù)存在于函數(shù)內(nèi)部,在沒有設(shè)定值時(shí)使用默認(rèn)值。
> plot(iris[,1],iris[,3],col = iris[,5]) #plot是高級(jí)繪圖函數(shù)
> text(6.5,4, labels = 'hello')#text是低級(jí)繪圖函數(shù),加標(biāo)注的
#label為繪圖參數(shù)
plot最古老最丑
ggplot背景為灰色,圖注在右側(cè)
ggpubr簡(jiǎn)化/美化,圖注在上方
ggplot2語法
1.入門級(jí)繪圖模版:作圖數(shù)據(jù),橫縱坐標(biāo)
> library(ggplot2)
> test = iris
> ggplot(data = test)+
+ geom_point(mapping = aes(x = Sepal.Length,
+ y = Petal.Length))
2.手動(dòng)設(shè)置與映射
2.1 手動(dòng)設(shè)置,需要設(shè)置為有意義的值
顏色color 大小 size 形狀shape 透明度 alpha 填充顏色fill
> ggplot(data = mpg) +
+ geom_point(mapping = aes(x = displ, y = hwy),
+ color = "blue")#color是geompoint的參數(shù),是具體顏色
> ggplot(data = mpg) +
+ geom_point(mapping = aes(x = displ, y = hwy),
+ size = 5, # 點(diǎn)的大小5mm
+ alpha = 0.5, # 透明度 50%
+ shape = 8) # 點(diǎn)的形狀可以為數(shù)字或手動(dòng)敲出來的形狀
> #手動(dòng)設(shè)置只能設(shè)置出1種顏色
2.2 映射:按照數(shù)據(jù)框的某一列來定義圖的某個(gè)屬性
> ggplot(data = test)+
+ geom_point(mapping = aes(x = Sepal.Length,
+ y = Petal.Length,
+ color = Species))#color是aes的參數(shù),是列名
Q1 能不能自行指定映射的具體顏色?
> ggplot(data = test)+
+ geom_point(mapping = aes(x = Sepal.Length,
+ y = Petal.Length,
+ color = Species))+
+ scale_color_manual(values = c("blue","grey","red"))#指定映射的具體顏色
Q2 區(qū)分color和fill兩個(gè)屬性
Q2-1 空心形狀和實(shí)心形狀都用color設(shè)置顏色
> ggplot(data = test)+
+ geom_point(mapping = aes(x = Sepal.Length,
+ y = Petal.Length,
+ color = Species),
+ shape = 17) #17號(hào),實(shí)心的例子
> ggplot(data = test)+
+ geom_point(mapping = aes(x = Sepal.Length,
+ y = Petal.Length,
+ color = Species),
+ shape = 2) #2號(hào),空心的例子
Q2-2 既有邊框又有內(nèi)心的,才需要color(邊框)和fill(內(nèi)心)兩個(gè)參數(shù)
> ggplot(data = test)+
+ geom_point(mapping = aes(x = Sepal.Length,
+ y = Petal.Length,
+ color = Species),
+ shape = 24,
+ fill = "black") #22號(hào),雙色的例子
3.分面
單分面
> ggplot(data = test) +
+ geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) +
+ facet_wrap(~ Species) #按Species進(jìn)行分面
雙分面
> test$Group = sample(letters[1:5],150,replace = T)#新增1列
> ggplot(data = test) +
+ geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) +
+ facet_grid(Group ~ Species)#按Group分行,按Species分列
#練習(xí)6-1
> # 示例數(shù)據(jù):ggplot2中的數(shù)據(jù)集mpg
> # 1.分別以mpg的displ和hwy兩列作為橫縱坐標(biāo),畫點(diǎn)圖。
> library(ggplot2)
> test=mpg
> ggplot(data = mpg) +
+ geom_point(mapping = aes(x = displ, y = hwy))
> # 2.嘗試修改顏色或大小,從mpg數(shù)據(jù)框中任選可以用來分類的列。
> ggplot(data = mpg) +
+ geom_point(mapping = aes(x = displ,
+ y = hwy,
+ color=class))
> ggplot(data = mpg) +
+ geom_point(mapping = aes(x = displ,
+ y = hwy,
+ color=displ),size=1)
> # 3.根據(jù)class列來分面
> ggplot(data = mpg) +
+ geom_point(mapping = aes(x = displ, y = hwy,color=displ),size=1)+facet_wrap(~class)
> # 4.根據(jù)drv和cyl兩個(gè)變量來分面
> ggplot(data = mpg) +
+ geom_point(mapping = aes(x = displ,
+ y = hwy,
+ color=displ),size=1)+
+ facet_grid(drv~cyl)
4.幾何對(duì)象(約等于圖層)

image.png

image.png
> ggplot(data = test) +
+ geom_smooth(mapping = aes(x = Sepal.Length,
+ y = Petal.Length))+
+ geom_point(mapping = aes(x = Sepal.Length,
+ y = Petal.Length))
`geom_smooth()` using method = 'loess' and formula 'y ~ x'
> ggplot(data = test,mapping = aes(x = Sepal.Length, y = Petal.Length))+
+ geom_smooth()+
+ geom_point()
`geom_smooth()` using method = 'loess' and formula 'y ~ x'
5.統(tǒng)計(jì)變換
#5.統(tǒng)計(jì)變換-直方圖
> View(diamonds)
> table(diamonds$cut)
Fair Good Very Good Premium Ideal
1610 4906 12082 13791 21551
> ggplot(data = diamonds) +
+ geom_bar(mapping = aes(x = cut))
> ggplot(data = diamonds) +
+ stat_count(mapping = aes(x = cut))
統(tǒng)計(jì)變換使用場(chǎng)景
5.1.不統(tǒng)計(jì),數(shù)據(jù)直接做圖
> fre = as.data.frame(table(diamonds$cut))
> fre
Var1 Freq
1 Fair 1610
2 Good 4906
3 Very Good 12082
4 Premium 13791
5 Ideal 21551
> ggplot(data = fre) +
+ geom_bar(mapping = aes(x = Var1, y = Freq), stat = "identity")
5.2 count改為prop
> ggplot(data = diamonds) +
+ geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))
6.位置調(diào)整
6.1抖動(dòng)的點(diǎn)圖
> ggplot(data = mpg,mapping = aes(x = class,
+ y = hwy,
+ group = class)) +
+ geom_boxplot()+
+ geom_point()
> ggplot(data = mpg,mapping = aes(x = class,
+ y = hwy,
+ group = class)) +
+ geom_boxplot()+
+ geom_jitter()
> ggplot(data = mpg,mapping = aes(x = class,
+ y = hwy,
+ group = class)) +
+ geom_boxplot()+
+ geom_dotplot(binaxis = "y",binwidth = .5,stackdir = "center")
6.2堆疊直方圖
> ggplot(data = diamonds) +
+ geom_bar(mapping = aes(x = cut,fill=clarity))
6.3 并列直方圖
> ggplot(data = diamonds) +
+ geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")
7.坐標(biāo)系
翻轉(zhuǎn)coord_flip()
> ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
+ geom_boxplot() +
+ coord_flip()
極坐標(biāo)系coord_polar()
> bar <- ggplot(data = diamonds) +
+ geom_bar(
+ mapping = aes(x = cut, fill = cut),
+ show.legend = FALSE,
+ width = 1
+ ) +
+ theme(aspect.ratio = 1) +
+ labs(x = NULL, y = NULL)
> bar + coord_flip()
> bar + coord_polar()