您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“如何使用R語言繪制散點圖結(jié)合邊際分布圖”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“如何使用R語言繪制散點圖結(jié)合邊際分布圖”這篇文章吧。
主要使用ggExtra
結(jié)合ggplot2
兩個R包進行繪制。(勝在簡潔方便)使用cowplot
與ggpubr
進行繪制。(勝在靈活且美觀)
下面的繪圖我們均以iris數(shù)據(jù)集為例。
# 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
下面我們一行代碼添加邊際分布(分別以密度曲線與直方圖的形式來展現(xiàn)):
# marginal plot: density ggMarginal(p, type = "density", groupColour = TRUE, groupFill = TRUE)
# marginal plot: histogram ggMarginal(p, type = "histogram", groupColour = TRUE, groupFill = TRUE)
# marginal plot: boxplot ggMarginal(p, type = "boxplot", groupColour = TRUE, groupFill = TRUE)
# marginal plot: violin ggMarginal(p, type = "violin", groupColour = TRUE, groupFill = TRUE)
# marginal plot: densigram ggMarginal(p, type = "densigram", groupColour = TRUE, groupFill = TRUE)
# 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
① 密度函數(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))
② 未被壓縮的箱線圖
# 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))
# 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é)合邊際分布圖”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(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)容。