Recently, in a tweet post, Claus Wilke provides the following solution for creating a perfect scatter plot with marginal density plots or histogram plots: library(cowplot) The problem with the above plots, is the presence of extra spaces between the main plot and the marginal density plots. Yplot <- ggboxplot(iris, x = "Species", y = "Sepal.Width", Xplot <- ggboxplot(iris, x = "Species", y = "Sepal.Length",Ĭolor = "Species", fill = "Species", palette = "jco", # Marginal boxplot of x (top panel) and y (right panel) Size = 3, alpha = 0.6, ggtheme = theme_bw()) Rel_widths = c(2, 1), rel_heights = c(1, 2))Īdd marginal boxplot: # Scatter plot colored by groups ("Species") Plot_grid(xplot, NULL, sp, yplot, ncol = 2, align = "hv", Xplot <- xplot + clean_theme() + rremove("legend") Yplot <- yplot + clean_theme() + rremove("legend") Yplot <- ggdensity(iris, "Sepal.Width", fill = "Species", Xplot <- ggdensity(iris, "Sepal.Length", fill = "Species", # Marginal density plot of x (top panel) and y (right panel) Sp <- ggscatter(iris, x = "Sepal.Length", y = "Sepal.Width", # Scatter plot colored by groups ("Species") In the R code below, we provide a solution using the cowplot package. One limitation of ggExtra is that it can’t cope with multiple groups in the scatter plot and the marginal plots. P <- ggscatter(iris, x = "Sepal.Length", y = "Sepal.Width", The function ggMarginal(), can be used to easily add a marginal histogram, density or boxplot to a scatter plot.įirst, install the ggExtra package as follow: install.packages(“ggExtra”) then type the following R code: # Add density distribution as marginal plot lect: character vector specifying some labels to show.To specify only the size and the style, use font.label = list(size = 14, face = “plain”). For example font.label = list(size = 14, face = “bold”, color =“red”).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |