03-08

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()
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 常用繪圖R包 1.基礎(chǔ)包 略顯陳舊 了解一下 2.ggplot2 中堅(jiān)力量 學(xué)起來有點(diǎn)難 3.ggpubr 江湖救...
    CyberPlanet閱讀 424評(píng)論 0 3
  • 復(fù)習(xí): 文件讀取 認(rèn)識(shí)csv格式 1.直接打開:切記要顯示“.csv”后綴 記事本打開image.png subl...
    喬幫主_d2ac閱讀 649評(píng)論 0 2
  • 常用可視化R包 *加粗為重點(diǎn)掌握 1. 作圖 1.1 基礎(chǔ)包:略顯陳舊,了解一下 (1) 繪圖函數(shù) 高級(jí)繪圖函數(shù) ...
    認(rèn)真學(xué)習(xí)小餅嘰閱讀 878評(píng)論 0 0
  • 1.常用可視化R包 作圖baseggplot2ggpubr 拼圖par里mfrowgrid.arragecowpl...
    不到7不改名閱讀 737評(píng)論 0 3
  • 需求論文中常見的分組箱型圖和分組條形圖可以直觀的比較方法的效果,以一個(gè)圖顯示多個(gè)方法在多個(gè)數(shù)據(jù)集上的AUC或AUP...
    Seurat_Satija閱讀 7,512評(píng)論 0 9

友情鏈接更多精彩內(nèi)容