溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何使用R語言繪制散點圖結(jié)合邊際分布圖

發(fā)布時間:2022-03-03 14:14:08 來源:億速云 閱讀:394 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要為大家展示了“如何使用R語言繪制散點圖結(jié)合邊際分布圖”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“如何使用R語言繪制散點圖結(jié)合邊際分布圖”這篇文章吧。

    主要使用ggExtra結(jié)合ggplot2兩個R包進行繪制。(勝在簡潔方便)使用cowplotggpubr進行繪制。(勝在靈活且美觀)

    下面的繪圖我們均以iris數(shù)據(jù)集為例。

    1. 使用ggExtra結(jié)合ggplot2

    1)傳統(tǒng)散點圖

    # library
    library(ggplot2)
    library(ggExtra)
    
    # classic plot
    p <- ggplot(iris) +
      geom_point(aes(x = Sepal.Length, y = Sepal.Width, color = Species), alpha = 0.6, shape = 16) +  # alpha 調(diào)整點的透明度;shape 調(diào)整點的形狀
      theme_bw() +
      theme(legend.position = "bottom") + # 圖例置于底部
      labs(x = "Sepal Length", y = "Sepal Width") # 添加x,y軸的名稱
    p

    如何使用R語言繪制散點圖結(jié)合邊際分布圖

    下面我們一行代碼添加邊際分布(分別以密度曲線與直方圖的形式來展現(xiàn)):

    2)密度函數(shù)

    # marginal plot: density
    ggMarginal(p, type = "density", groupColour = TRUE, groupFill = TRUE)

    如何使用R語言繪制散點圖結(jié)合邊際分布圖

    3)直方圖

    # marginal plot: histogram
    ggMarginal(p, type = "histogram", groupColour = TRUE, groupFill = TRUE)

    如何使用R語言繪制散點圖結(jié)合邊際分布圖

    4)箱線圖(寬窄的顯示會有些問題)

    # marginal plot: boxplot
    ggMarginal(p, type = "boxplot", groupColour = TRUE, groupFill = TRUE)

    如何使用R語言繪制散點圖結(jié)合邊際分布圖

    5)小提琴圖(會有重疊,不建議使用)

    # marginal plot: violin
    ggMarginal(p, type = "violin", groupColour = TRUE, groupFill = TRUE)

    如何使用R語言繪制散點圖結(jié)合邊際分布圖

    6)密度函數(shù)與直方圖同時展現(xiàn)

    # marginal plot: densigram
    ggMarginal(p, type = "densigram", groupColour = TRUE, groupFill = TRUE)

    如何使用R語言繪制散點圖結(jié)合邊際分布圖

    2. 使用cowplot與ggpubr

    1)重繪另一種散點圖

    # Scatter plot colored by groups ("Species")
    sp <- ggscatter(iris, x = "Sepal.Length", y = "Sepal.Width",
                    color = "Species", palette = "jco",
                    size = 3, alpha = 0.6) +
      border() +
      theme(legend.position = "bottom")
    sp

    如何使用R語言繪制散點圖結(jié)合邊際分布圖

    2)有縫拼接

    ① 密度函數(shù)

    library(cowplot)
    # Marginal density plot of x (top panel) and y (right panel)
    xplot <- ggdensity(iris, "Sepal.Length", fill = "Species",
                       palette = "jco")
    yplot <- ggdensity(iris, "Sepal.Width", fill = "Species", 
                       palette = "jco") +
      rotate()
    
    # Cleaning the plots
    sp <- sp + rremove("legend")
    yplot <- yplot + clean_theme() + rremove("legend")
    xplot <- xplot + clean_theme() + rremove("legend")
    # Arranging the plot using cowplot
    plot_grid(xplot, NULL, sp, yplot, ncol = 2, align = "hv", 
              rel_widths = c(2, 1), rel_heights = c(1, 2))

    如何使用R語言繪制散點圖結(jié)合邊際分布圖

    ② 未被壓縮的箱線圖

    # Marginal boxplot of x (top panel) and y (right panel)
    xplot <- ggboxplot(iris, x = "Species", y = "Sepal.Length", 
                       color = "Species", fill = "Species", palette = "jco",
                       alpha = 0.5, ggtheme = theme_bw())+
      rotate()
    yplot <- ggboxplot(iris, x = "Species", y = "Sepal.Width",
                       color = "Species", fill = "Species", palette = "jco",
                       alpha = 0.5, ggtheme = theme_bw())
    # Cleaning the plots
    sp <- sp + rremove("legend")
    yplot <- yplot + clean_theme() + rremove("legend")
    xplot <- xplot + clean_theme() + rremove("legend")
    # Arranging the plot using cowplot
    plot_grid(xplot, NULL, sp, yplot, ncol = 2, align = "hv", 
              rel_widths = c(2, 1), rel_heights = c(1, 2))

    如何使用R語言繪制散點圖結(jié)合邊際分布圖

    3)無縫拼接

    # Main plot
    pmain <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
      geom_point() +
      color_palette("jco")
    # Marginal densities along x axis
    xdens <- axis_canvas(pmain, axis = "x") +
      geom_density(data = iris, aes(x = Sepal.Length, fill = Species),
                   alpha = 0.7, size = 0.2) +
      fill_palette("jco")
    # Marginal densities along y axis
    # Need to set coord_flip = TRUE, if you plan to use coord_flip()
    ydens <- axis_canvas(pmain, axis = "y", coord_flip = TRUE) +
      geom_density(data = iris, aes(x = Sepal.Width, fill = Species),
                   alpha = 0.7, size = 0.2) +
      coord_flip() +
      fill_palette("jco")
    p1 <- insert_xaxis_grob(pmain, xdens, grid::unit(.2, "null"), position = "top")
    p2 <- insert_yaxis_grob(p1, ydens, grid::unit(.2, "null"), position = "right")
    ggdraw(p2)

    如何使用R語言繪制散點圖結(jié)合邊際分布圖

    以上是“如何使用R語言繪制散點圖結(jié)合邊際分布圖”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

    向AI問一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI