使用R語言繪制散點圖結合邊際分佈圖教程
主要使用ggExtra
結合ggplot2
兩個R包進行繪制。(勝在簡潔方便)使用cowplot
與ggpubr
進行繪制。(勝在靈活且美觀)
下面的繪圖我們均以iris數據集為例。
1. 使用ggExtra結合ggplot2
1)傳統散點圖
# 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 調整點的透明度;shape 調整點的形狀 theme_bw() + theme(legend.position = "bottom") + # 圖例置於底部 labs(x = "Sepal Length", y = "Sepal Width") # 添加x,y軸的名稱 p
下面我們一行代碼添加邊際分佈(分別以密度曲線與直方圖的形式來展現):
2)密度函數
# marginal plot: density ggMarginal(p, type = "density", groupColour = TRUE, groupFill = TRUE)
3)直方圖
# marginal plot: histogram ggMarginal(p, type = "histogram", groupColour = TRUE, groupFill = TRUE)
4)箱線圖(寬窄的顯示會有些問題)
# marginal plot: boxplot ggMarginal(p, type = "boxplot", groupColour = TRUE, groupFill = TRUE)
5)小提琴圖(會有重疊,不建議使用)
# marginal plot: violin ggMarginal(p, type = "violin", groupColour = TRUE, groupFill = TRUE)
6)密度函數與直方圖同時展現
# marginal plot: densigram ggMarginal(p, type = "densigram", groupColour = TRUE, groupFill = TRUE)
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
2)有縫拼接
① 密度函數
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))
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)
參考
Articles – ggpubr: Publication Ready Plots——Perfect Scatter Plots with Correlation and Marginal Histograms
Marginal distribution with ggplot2 and ggExtra
以上就是使用R語言繪制散點圖結合邊際分佈圖教程的詳細內容,更多關於R語言繪制散點圖結合邊際分佈圖的資料請關註WalkonNet其它相關文章!
推薦閱讀:
- R語言數據可視化ggplot繪制置信區間與分組繪圖技巧
- R語言數據預處理操作——離散化(分箱)
- Python可視化學習之seaborn繪制線型回歸曲線
- R語言實現支持向量機SVM應用案例
- Python matplotlib繪制散點圖配置(萬能模板案例)