久久精品精选,精品九九视频,www久久只有这里有精品,亚洲熟女乱色综合一区
    分享

    【R分享|實戰】 跟著科白君學畫散點圖就對了~

     科白君 2021-07-22



     夯實基礎,持之以恒。”   --科白君


    "R實戰"專題·第1篇
      編輯 科白維尼
      5311字 | 9分鐘閱讀

    本文推送內容
    利用ggplot2包及相關包描繪美觀且有用的散點圖。散點圖是一種常用的圖形,可以直觀展示回歸分析中數據的分布和聚合情況(因變量隨自變量而變化的大致趨勢,進而找到變量之間的合適函數關系)。我們經常看到用散點圖表示線性回歸關系,進行預測分析,進而做出科學的決策。變量之間的關系有很多,如線性關系、指數關系、對數關系等等,當然,沒有關系也是一種重要的關系。

    本文將逐步講解,教大家如何用ggplot2包等繪制讓人賞心悅目的散點圖,基于R-4.0.2版本。
    首先,加載所需R包。
    if (!require("pacman")) install.packages ("pacman")  # 下載 pacman 程序包 
    library ("pacman") # library 加載 pacman 程序包
    p_load 
    (ggplot2, ggthemes, dplyr, readr, showtext, export) # p_load 需要 pacman 包才能運行

    然后,我們利用R語言自帶的數據集"mpg"。

    # 選擇R語言自帶的數據集,<- 為賦值符號
    <mpg
    # 查看數據集前 行,tail () 查看尾 行 
    head (a)
    # 如果想看前 10 行的數據,可以改寫成 head (a, 10)
    # 如果想深入了解該函數用法,可以用 help () 進行訪問
    # 了解數據結構 
    str (a)

    結果展示:

    最后,利用這份數據,逐步繪制和完善散點圖。

    3.1 繪制基本的散點圖

    p1 <- ggplot (data = a,    # ggplot 函數基礎下能夠畫多種多樣的圖形
       
    aes (x = hwy, y = displ, colour = class))+  # aes 描繪函數,對載入數據中所要表達的x, y, colour, shape等等
     
    geom_point ( ) # 畫散點圖的函數 geom_point ( ) 
    p1 # 或者 print (p1)

    輸出結果:


    在這個基礎圖上,我們如何進行修改和完善?我們畫圖的目的是讓圖片的比例、顏色、形狀更讓人賞心悅目。接下來,我將逐步對散點圖主要的元素進行描繪和講解,以便大家學習和模仿。

    3.2 調整點的大小

    圖中每個散點有點小,不便于大家閱讀,將其放大一些。

    p2 <- ggplot (data a, 
     
    aes (x hwy, y displ, colour class))+
      
    geom_point (size = 3.5) # size 函數作用是改變大小
    p2  

    輸出結果:


    3.3 根據不同的分組改變散點圖的形狀

    p3 <- ggplot (data = a,
     
    aes (x = hwy, y = displ, colour = class, shape = class))+
     
     geom_point (size 3.5)+
      
    scale_shape_manual (values =(16, 17, 18, 19, 20, 21, 22)) # scale_shape_manual 函數用來自定義對應分組散點的形狀,數字表示形狀類型
    p3 

    輸出結果:


    我們發現既對class進行顏色區分,又對其進行形狀區分,這樣的視覺效果并不好。于是,我們只保留其顏色區分,并選用18號充實菱形形狀來描繪。

    p4 <- ggplot (data = a,
     
    aes (x = hwy, y = displ, colour = class))+
      
    geom_point (size 5, shape = 18) # 在 geom_point 函數下的 shape 表示所有點都為該形狀
    p4 

    結果展示:


    其實在用我們自己實驗得到的數據時,特別是平行樣品較多的時候,很多樣點會重疊看不清楚,這里我們可以調節散點的透明度,讓重疊的點也變得可讀。

    3.4 調節散點圖的透明度

    這里需要用到 geom_point 里的 alpha 函數

    p5 <- ggplot (data = a,
      aes
    (x = hwy, y = displ, colour = class))+
      geom_point
    (size 5, shape =18, alpha 0.5) alpha 函數作用是調節圖形中元素的透明度
    p5

    結果展示:


    3.5 修改x,y軸的刻度范圍

    圖基本調節好了,但發現x,y軸有點參差不齊。x和y軸的刻度不是我們所希望表現的,需要重新設定。這里,需要用到 scale_x/y_continuous 函數。

    p6 <- p5+  
      scale_x_continuous
    (limits = c (10, 45), breaks = seq (10, 45, 5))+ breaks 函數表示 min,max,刻度間隔
      scale_y_continuous
    (limits = c (1, 7), breaks = seq (1, 7, 1))  limits 函數表示 min, max
    p6

    結果展示:


    3.6 設定x,y軸的標簽及標題和副標等

    這時候我們可以根據自己的需求修改x,y軸的標簽名,以及給圖片附上標題和副標,讓圖片更加完整。

    p7 <- p6+  
      labs
    (title "Changes of soil organic carbon with soil depth"# labs 函數用來寫標簽,記得必須添加引號
               subtitle
    "Soil organic carbon")+
      labs
    (x "soil depth", y "Soil organic carbon content (g/kg)")
    p7

    結果展示:

    3.7 圖例的管理(這期主要講位置)

    圖例比較小,單獨放在外面不夠美觀。仔細觀察,圖例右上角還有一處空白地,咱們就把它移動進去,這樣整張圖就顯得更加飽滿了。不過圖例具體如何管理還需要根據畫出來的圖片情況來調整,有時可以將圖例放圖頂或者圖底,因人而異。這里,需要強調,當圖例放進圖中時,x, y為(0,0)是左下角,(1,1)是右上角。根據自己情況進行調整。

    p8 <- p7+
      theme
    ( legend.position = c (0.8, 0.6)) theme 函數主要是圖片內的主題等 
    p8

    結果展示:

    我們發現得到的圖里有一塊白色的,不太美觀,再用一條圖例背景代碼。

    p9 <- p8+
      theme
    (legend.background = element_blank ())
    p9

    得到:

    關于圖例的其他設置,我會專門找一期來細講。

    3.8 字體設置

    可以根據自己喜好調整字體,但是一般投稿的字體類型都用的Arial字體。R語言輸出的圖片默認的也是Arial字體。
    windowsFonts (
      
    # 中文字體
      lishu
    = windowsFont (family "LiSu"), # 隸書
      yahei
    = windowsFont (family "Microsoft YaHei"), # 微軟雅黑
      xinwei
    = windowsFont (family "STXingwei"), # 華文新魏
      kaiti
    = windowsFont (family "KaiTi"), # 楷體
      heiti
    = windowsFont (family "SimHei"), # 黑體
      
    # 英文字體
      arial
    = windowsFont (family "Arial"), # Arial字體
      newman
    = windowsFont (family "Times New Roman"), #Times New Roman字體
      hand
    = windowsFont (family "Lucida Calligraphy"), # Lucida手寫體
      Helvetica
    = windowsFont (family "Helvetica")# 印刷體 
    )

    3.9 字體大小和類型

    仔細觀察,現在得到的圖x, y軸的標簽以及字體大小都偏小,因此需要進行修改。

    p9_1 <- ggplot (data = a,
     aes
    (x = hwy, y = displ, colour = class))+
      geom_point
    (size 5, alpha 0.5, shape 18)+
      scale_x_continuous
    (limits = c (10, 45), breaks = seq (10, 45, 5))+
      scale_y_continuous
    (limits = c (1, 7), breaks = seq (171))+
      theme
    (axis.text = element_text (size 12, colour "black"), # axis.text/title 表示坐標軸標簽的信息
      axis.title
    = element_text (size =12, colour "black"), # 坐標軸的顏色默認是灰黑色并不是黑色,這里需要自己改成黑色。
      legend.position
    = c (0.80.6),
      legend.background
    = element_blank (),
      legend.text
    = element_text (size 12), # 圖例信息的大小
      text
    = element_text (family ="arial"),
      plot.title
    = element_text (family ="arial", size 16))+ # family 函數表示字體類型,size 表示大小 
      labs
    (title "Changes of soil organic carbon with soil depth",
      subtitle
    "Soil organic carbon")+
      labs
    ( x ="soil depth", y = "Soil organic carbon content (g/kg)")
    p9_1

    結果展示:

    我不太喜歡R語言自帶的背景色,通常都選用網格白色底。需要用到theme_bw () 函數。

    p9_1 <- ggplot (data = a,
     aes
    (x = hwy, y = displ, colour = class))+
      geom_point
    (size 5, alpha 0.5, shape 18)+
      scale_x_continuous
    (limits = c (10, 45), breaks = seq (10, 45, 5))+
      scale_y_continuous
    (limits = c (1, 7), breaks = seq (171))+ theme_bw ( )+ 
      theme
    (axis.text = element_text (size 12, colour "black"), # axis.text/title 表示坐標軸標簽的信息
      axis.title
    = element_text (size =12, colour "black"), # 坐標軸的顏色默認是灰黑色并不是黑色,這里需要自己改成黑色。
      legend.position
    = c (0.80.6)
      legend.background
    = element_blank (),
      legend.text
    = element_text (size 12), # 圖例信息的大小
      text
    = element_text (family ="arial"),
      plot.title
    = element_text (family ="arial", size 16))+ # family 函數表示字體類型,size 表示大小 
      labs
    (title "Changes of soil organic carbon with soil depth",
      subtitle
    "Soil organic carbon")+
      labs
    ( x ="soil depth", y = "Soil organic carbon content (g/kg)"
    p9_1

    關于背景主題的題材很多,這一期就不展開講了。后面找一專題再具體詳聊。

    4.0 輸出圖片
    這里,我們可以用 ggplot2 包的 ggsave 函數或者 export 包的 graph2ppt 函數。

    # export 包下的輸出方式
    p9_2 <- p9_1+
    graph2ppt
    (file ="outcome.ppt", append FALSE)

    # ggplot2 包下的輸出方式
    p9_3
    <- ggplot (data = a,
     aes
    (x = hwy, y = displ, colour = class))+
      geom_point
    (size 5, alpha 0.5, shape 18)+
      scale_x_continuous
    (limits = c (10, 45), breaks = seq (10, 45, 5))+
      scale_y_continuous
    (limits = c (1, 7), breaks = seq (1, 7, 1))+ theme_bw ( )+
      theme
    (axis.text = element_text (size 12, colour = "black"),  # axis.text/title 表示坐標軸標簽的信息
       axis.title
    = element_text (size 12, colour "black"),  # 坐標軸的顏色默認是灰黑色并不是黑色,這里需要自己改成黑色。
       legend.position
    = c (0.8, 0.6),
       legend.background
    = element_blank (),
       legend.text
    = element_text (size = 12)# 圖例信息的大小
       plot.title
    element_text (size = 16))+ # size 表示大小
      labs
    (title = "Changes of soil organic carbon with soil depth",
      subtitle
    "Soil organic carbon")+
      labs
    ( x "soil depth", y ="Soil organic carbon content (g/kg)")
    ggsave
    ("p9_3.pdf") # 由于在輸出是出現了無效字體的報錯,只能把有關字體的代碼刪除了再進行輸出。

    附上完整的代碼:

    #####################################
    # ggplot2 包畫散點圖的詳細講解與繪制
    #####################################

    #1 加載所需要的R包
    if (!require("pacman")) install.packages ("pacman") # 下載 pacman 程序包
    library ("pacman")
    # library 加載 pacman 程序包
    p_load (ggplot2, ggthemes, dplyr, readr, showtext, export) # p_load 需要 pacman 包才能運行

    #2 加載數據集
    # 選擇R語言自帶的數據集,<- 為賦值符號
    a <- mpg
    # 查看數據集前 6 行,tail () 查看尾 6 行
    head (a)
    # 如果想看前 10 行的數據,可以改寫成 head (a, 10)
    # 如果想深入了解該函數用法,可以用 help () 進行訪問
    # 了解數據結構
    str (a)

    #3 逐步分析講解散點圖
    #1) 基礎的散點圖
    p1 <- ggplot (data = a, # ggplot 函數基礎下能夠畫多種多樣的圖形
      aes (x = hwy, y = displ, colour = 
    class))+ # aes 描繪函數,對載入數據中所要表達的x, y, colour, shape等等
      geom_point ( ) # 畫散點圖的函數 geom_point ( )
    p1 # 或者 print (p1)

    #2) 調整點的大小
    p2 <- ggplot (data = a,
    aes (x = hwy, y = displ, colour =
    class))+
      geom_point (size =
    3.5) # size 函數作用是改變大小
    p2

    #3) 根據不同的分組改變散點圖的形狀
    p3 <- ggplot (data = a,
      aes (x = hwy, y = displ, colour =
    class, shape = class))+
      geom_point (size = 3.5)+
      scale_shape_manual (values = c (16, 17, 18, 19, 20, 21, 22)) # scale_shape_manual 函數用來自定義對應分組散點的形狀,數字表示形狀類型
    p3

    #4) 只保留其顏色區分,并選用18號充實菱形形狀來描繪
    p4 <- ggplot (data = a,
      aes (x = hwy, y = displ, colour =
    class))+
      geom_point (size = 5, shape = 18) # 在 geom_point 函數下的 shape 表示所有點都為該形狀
    p4

    #5) 調節散點圖的透明度
    p5 <- ggplot (data = a,
      aes (x = hwy, y = displ, colour =
    class))+
      geom_point (size = 5, shape =18, alpha = 0.5) # alpha 函數作用是調節圖形中元素的透明度
    p5

    #6) 修改x,y軸的刻度范圍
    p6 <- p5+
      scale_x_continuous (limits = c (10, 45), breaks = seq (10, 45, 5))+ # breaks 函數表示 min,max,刻度間隔
      scale_y_continuous (limits = c (1, 7), breaks = seq (1, 7, 1)) # limits 函數表示 min, max
    p6

    #7) 設定x,y軸的標簽及標題和副標等
    p7 <- p6+
      labs (title = "Changes of soil organic carbon with soil depth", # labs 函數用來寫標簽,記得必須添加引號
      subtitle = "Soil organic carbon")+
      labs (x = "soil depth", y = "Soil organic carbon content (g/kg)")
    p7

    #8) 圖例的管理(這期主要講位置)
    p8 <- p7+
      theme ( legend.position = c (0.8, 0.6)) # theme 函數主要是圖片內的主題等
    p8
    p9 <- p8+
      theme (legend.background = element_blank ())
    p9

    #9) 字體設置
    windowsFonts (
      # 中文字體
      lishu = windowsFont (family = "LiSu"), # 隸書
      yahei = windowsFont (family = "Microsoft YaHei"), # 微軟雅黑
      xinwei = windowsFont (family = "STXingwei"), # 華文新魏
      kaiti = windowsFont (family = "KaiTi"), # 楷體
      heiti = windowsFont (family = "SimHei"), # 黑體
      # 英文字體
      arial = windowsFont (family = "Arial"), # Arial字體
      newman = windowsFont (family = "Times New Roman"), #Times New Roman字體
      hand = windowsFont (family = "Lucida Calligraphy"), # Lucida手寫體
      Helvetica = windowsFont (family = "Helvetica"), # 印刷體
    )

    #10) 字體大小和類型
    p9_1 <- ggplot (data = a,
     aes (x = hwy, y = displ, colour =
    class))+
      geom_point (size = 5, alpha = 0.5, shape = 18)+
      scale_x_continuous (limits = c (10, 45), breaks = seq (10, 45, 5))+
      scale_y_continuous (limits = c (1, 7), breaks = seq (1, 7, 1))+
      theme (axis.text = element_text (size = 12, colour = "black"), # axis.text/title 表示坐標軸標簽的信息
       axis.title = element_text (size =12, colour = "black"), # 坐標軸的顏色默認是灰黑色并不是黑色,這里需要自己改成黑色。
       legend.position = c (0.8, 0.6),
       legend.background = element_blank (),
       legend.text = element_text (size = 12), # 圖例信息的大小
       text = element_text (family ="arial"),
       plot.title = element_text (family ="arial", size = 16))+ # family 函數表示字體類型,size 表示大小
      labs (title = "Changes of soil organic carbon with soil depth",
      subtitle = "Soil organic carbon")+
      labs ( x ="soil depth", y = "Soil organic carbon content (g/kg)")
    p9_1

    # 更換白色底背景
    p9_1 <- ggplot (data = a,
     aes (x = hwy, y = displ, colour =
    class))+
      geom_point (size = 5, alpha = 0.5, shape = 18)+
      scale_x_continuous (limits = c (10, 45), breaks = seq (10, 45, 5))+
      scale_y_continuous (limits = c (1, 7), breaks = seq (1, 7, 1))+
      theme_bw ( )+
      theme (axis.text = element_text (size = 12, colour = "black"), # axis.text/title 表示坐標軸標簽的信息
       axis.title = element_text (size =12, colour = "black"), # 坐標軸的顏色默認是灰黑色并不是黑色,這里需要自己改成黑色。
       legend.position = c (0.8, 0.6),
       legend.background = element_blank (),
       legend.text = element_text (size = 12), # 圖例信息的大小
       text = element_text (family ="arial"),
       plot.title = element_text (family ="arial", size = 16))+ # family 函數表示字體類型,size 表示大小
      labs (title = "Changes of soil organic carbon with soil depth",
      subtitle = "Soil organic carbon")+
      labs ( x ="soil depth", y = "Soil organic carbon content (g/kg)")
    p9_1

    # 輸出圖片
    # export 包
    p9_2 <- p9_1+
    graph2ppt(file="outcome.ppt",append=FALSE)
    # ggplot2 包
    p9_3 <- ggplot (data = a,
     aes (x = hwy, y = displ, colour =
    class))+
      geom_point (size = 5, alpha = 0.5, shape = 18)+
      scale_x_continuous (limits = c (10, 45), breaks = seq (10, 45, 5))+
      scale_y_continuous (limits = c (1, 7), breaks = seq (1, 7, 1))+ theme_bw ( )+
      theme (axis.text = element_text (size = 12, colour = "black"), # axis.text/title 表示坐標軸標簽的信息
       axis.title = element_text (size =12, colour = "black"), # 坐標軸的顏色默認是灰黑色并不是黑色,這里需要自己改成黑色。
       legend.position = c (0.8, 0.6),
       legend.background = element_blank (),
       legend.text = element_text (size = 12), # 圖例信息的大小
       plot.title = element_text (size = 16))+ # size 表示大小
      labs (title = "Changes of soil organic carbon with soil depth",
      subtitle = "Soil organic carbon")+
      labs ( x ="soil depth", y = "Soil organic carbon content (g/kg)")
    ggsave("p9_1.pdf")

    最后展示

    Tips:
    1)R語言的每行代碼也如同PS里的圖層一樣,如果這行代碼放在下一行可能出來的結果就不一樣了。例如,theme_bw ( ) 這個代碼如果放在最后又是另一個結果。本文推送沒有展示,大家可以自己試試。
    2)所有不懂的函數,或者需要詳細了解的函數,都可以用 help (" ") 進行訪問,或者利用 ? " xx "也可以。
    3)代碼可以直接copy,但是也希望自己多敲敲代碼喔,畢竟熟能生巧嘛~

      轉藏 分享 獻花(0

      0條評論

      發表

      請遵守用戶 評論公約

      類似文章 更多

      主站蜘蛛池模板: 国产成人高清亚洲综合| 久久97人人超人人超碰超国产| 免费无码又爽又刺激高潮虎虎视频| 久久精品国产亚洲AV麻| 亚洲中文在线精品国产| 欧洲精品色在线观看| JAPANESE国产在线观看播放| 婷婷久久综合九色综合88| 翘臀少妇被扒开屁股日出水爆乳| 国内大量揄拍人妻精品視頻 | 中文字幕AV无码一二三区电影| 91福利一区福利二区| 国产精品色内内在线播放| 国产精品一码二码三码| 亚洲中文字幕无码专区| 亚洲中文字幕国产精品| 免费AV手机在线观看片| 欧美不卡无线在线一二三区观| 中文无码久久精品| 亚洲男人AV天堂午夜在| 永久黄网站色视频免费直播| 人妻系列无码专区69影院| 亚洲乱色熟女一区二区三区麻豆| 欧美日韩一区二区综合| 男女激情一区二区三区| 大香区一二三四区2021| 国产成人8X人网站视频| 中国熟女仑乱hd| 精品麻豆国产色欲色欲色欲WWW| 日韩中文字幕人妻一区| 精品国产乱码久久久久APP下载| 性虎精品无码AV导航| 国产精品日韩中文字幕| 精品人妻无码专区在中文字幕| 天堂亚洲免费视频| 久久精品免视看国产成人| 无码国产精品一区二区免费式影视| 国产中文三级全黄| 欧洲亚洲精品免费二区| 一本一道久久A久久精品综合| 欧美成 人影片 免费观看|