01
—
ggExtra/ggpubr:边际密度示意图
ggplot拓展功能
第一部分的添加边际密度示意图。
#-----ggExtra:Add marginal density plots-------#
library(ggplot2)
# Create a scatter plot
p <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point(aes(color = Species), size = 3, alpha = 0.6) +
scale_color_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))
# Add density distribution as marginal plot
# install.packages("ggExtra")
library("ggExtra")
ggMarginal(p, type = "density")
# Change marginal plot type
ggMarginal(p, type = "boxplot")
#-----ggpubr-------#
# One limitation of ggExtra is that it can’t cope with multiple groups in the scatter plot and the marginal plots.
# A solution is provided in the function ggscatterhist() [ggpubr]:
library(ggpubr)
# Grouped Scatter plot with marginal density plots
ggscatterhist(
iris, x = "Sepal.Length", y = "Sepal.Width",
color = "Species", size = 3, alpha = 0.6,
palette = c("#00AFBB", "#E7B800", "#FC4E07"),
margin.params = list(fill = "Species", color = "black", size = 0.2)
)
# Use box plot as marginal plots
ggscatterhist(
iris, x = "Sepal.Length", y = "Sepal.Width",
color = "Species", size = 3, alpha = 0.6,
palette = c("#00AFBB", "#E7B800", "#FC4E07"),
margin.plot = "boxplot",
ggtheme = theme_bw()
)
02
—
ggforce:局部放大
ggplot拓展功能
第二部分的局部放大。
#-----ggforce:Zoom in a scatter plot-------#
# Key function: facet_zomm() [in ggforce] (Pedersen 2016).
# Demo data set: iris. The R code below zoom the points where Species == "versicolor".
library(ggforce)
ggplot(iris, aes(Petal.Length, Petal.Width, colour = Species)) +
geom_point() +
ggpubr::color_palette("jco") +
facet_zoom(x = Species == "versicolor")+
theme_bw()
# x varible control
ggplot(iris, aes(Petal.Length, Petal.Width, colour = Species)) +
geom_point() +
ggpubr::color_palette("jco") +
facet_zoom(x = Petal.Length < 2.5)+
theme_bw()
# ylim control
ggplot(iris, aes(Petal.Length, Petal.Width, colour = Species)) +
geom_point() +
ggpubr::color_palette("jco") +
facet_zoom(ylim = c(1,2))+
theme_bw()
具体指引详见
http://www.sthda.com/english/articles/32-r-graphics-essentials/131-plot-two-continuous-variables-scatter-graph-and-alternatives/