欢迎大家关注全网生信学习者系列:
使用 ggplot2
包画箱线图通常使用 geom_boxplot()
函数。箱线图(Boxplot)是一种用于展示一组数据分布特征的图形,它能够提供以下信息:
箱线图非常适合用于比较不同组别的数据分布情况,例如,比较不同治疗方法的效果,或者不同群体的某个指标的分布。
在ggplot2 中,可以通过使用 geom_point() 来在箱线图上增加点,这些点可以代表分组中特定指标的出现率。
patternplot 是一个 R 包,它提供了创建网格状箱线图的功能,这种图表通常用于展示多个组别或条件下的数据分布。patternplot 包的 pattern_boxplot() 函数可以生成这样的图形,它允许用户在网格中为每个子集绘制箱线图,从而可以直观地比较不同组别或条件下的数据。下面是box1参数的详细解释:
library(patternplot)
library(png)
library(ggplot2)
library(gridExtra)
data <- read.csv(system.file("extdata", "fruits.csv", package = "patternplot"))
group <- data$Fruit
y <- data$Weight
x <- data$Store
pattern.type <- c('nwlines', 'blank', 'waves')
pattern.color <- c('black','black', 'black')
background.color <- c('white','gray80', 'white')
frame.color <- c('black', 'black', 'black')
pattern.line.size <- c(6, 1,6)
density <- c(6, 1, 8)
box1 <- patternboxplot(
data, x, y, group = group,
pattern.type = pattern.type,
pattern.line.size = pattern.line.size,
label.size = 3,
pattern.color = pattern.color,
background.color = background.color,
frame.color = frame.color,
density = density,
legend.h = 2,
legend.x.pos = 1.075,
legend.y.pos = 0.499,
legend.pixel = 10,
legend.w = 0.18,
legend.label = c("Orange","Strawberry","Watermelon")) +
ggtitle('(A) Boxplot with Black and White Patterns')
pattern.color <- c('black','white', 'grey20')
background.color <- c('gold','lightpink', 'lightgreen')
box2 <- patternboxplot(
data, x, y, group = group,
pattern.type = pattern.type,
pattern.line.size = pattern.line.size,
label.size = 3,
pattern.color = pattern.color,
background.color = background.color,
frame.color = frame.color,
density = density,
legend.h = 2,
legend.x.pos = 1.075,
legend.y.pos = 0.499,
legend.pixel = 10,
legend.w = 0.18,
legend.label = c("Orange","Strawberry","Watermelon")) +
ggtitle('(B) Boxplot with Colors and Patterns')
grid.arrange(box1, box2, nrow = 1)
结果:不同网格展示的分组的箱线图
library(tidyverse)
library(ggplot2)
rm(list = ls())
options(stringsAsFactors = F)
data("iris")
head(iris)
Sepal.Length<dbl> | Sepal.Width<dbl> | Petal.Length<dbl> | Petal.Width<dbl> | Species<fctr> | |
---|---|---|---|---|---|
1 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
2 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
3 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
4 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
5 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
6 | 5.4 | 3.9 | 1.7 | 0.4 | setosa |
Sepal.Length
指标和Species
分组Sepal.Length
在每个分组的出现率和对应画图位置坐标plotdata <- iris |>
dplyr::select(Sepal.Length, Species) |>
dplyr::mutate(Species = factor(Species, c("setosa", "versicolor", "virginica"))) |>
dplyr::rename(Group = Species,
Index = Sepal.Length)
occ_cutoff <- 5.2
occ_fun <- function(x) {
return(round(length(x[x > occ_cutoff])/length(x), 4))
}
plotOcc <- plotdata |>
dplyr::group_by(Group) |>
dplyr::summarise(occ = occ_fun(Index)) |>
dplyr::mutate(occ_lab = paste0(occ * 100, "%")) |>
dplyr::mutate(position = min(plotdata$Index) - min(plotdata$Index) * 0.1)
head(plotOcc)
Group<fctr> | occ<dbl> | occ_lab<chr> | position<dbl> | |
---|---|---|---|---|
setosa | 0.22 | 22% | 3.87 | |
versicolor | 0.90 | 90% | 3.87 | |
virginica | 0.98 | 98% | 3.87 |
采用ggplot2
的画图,下面是代码的逐行解析:
ggplot(data = plotdata, aes(x = Group, y = Index, color = Group))
:
plotdata
数据框,并设置分组变量 Group
映射到 x 轴,指标 Index
映射到 y 轴,同时 Group
映射到颜色,用于区分不同组的颜色。stat_boxplot(geom = "errorbar", width = 0.15)
:
errorbar
几何对象,宽度设置为 0.15,这可能是用来表示箱线图的中位数线或四分位数范围的误差条。geom_boxplot(width = .4, outlier.shape = NA)
:
geom_point(size = 2, shape = 5)
:
labs(x = "")
:
scale_y_continuous(expand = expansion(mult = c(0.1, 0.1)))
:
mult
参数设置为 0.1,意味着在 y 轴的两端各增加 10% 的空间。geom_point(data = plotOcc, aes(x = Group, y = position, size = occ), show.legend = FALSE, shape = 1, stroke = 1)
:
plotOcc
数据框添加点,这些点表示出现率,映射 Group
到 x 轴,position
到 y 轴,occ
到点的大小,不显示图例,点的形状设置为 1(圆形),并且有 1 的描边。geom_text(data = plotOcc, aes(x = Group, y = position, label = occ_lab), show.legend = FALSE)
:
plotOcc
数据框的基础上添加文本标签,映射 Group
到 x 轴,position
到 y 轴,occ_lab
作为标签文本,不显示图例。scale_size_continuous(range = c(10, 12))
:
coord_flip()
:
guides(color = "none")
:
theme_classic()
:
theme(axis.title = element_text(size = 12, color = "black", face = "bold"), axis.text = element_text(size = 10, color = "black"), text = element_text(size = 9, color = "black"))
:
pl
结果:带有显示Index在不同分组出现率的箱线图
结果:带有显示Index在不同分组出现率的prism风格的箱线图
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。