绘制高颜值的云雨图

2023-03-18 18:51:05 浏览数 (1)

#R包下载与载入:

library(remotes)

remotes::install_github('jorvlan/raincloudplots')

library(raincloudplots)

library(ggplot2)

##

#绘图数据整理:

head(iris) #以鸢尾数据集为例;

# Sepal.Length Sepal.Width Petal.Length Petal.Width Species

# 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

#需使用特定函数转化为绘图所需格式:

dt1 <- data_1x1(

array_1 = iris$Sepal.Length[1:50], #分组1

array_2 = iris$Sepal.Length[51:100], #分组2

jit_distance = 0.09, #抖动点间的距离

jit_seed = 123#设置随机数种子

)

head(dt1)

# y_axis x_axis id jit

# 1 5.1 1 1 0.9617640

# 2 4.9 1 2 1.0518949

# 3 4.7 1 3 0.9836158

# 4 4.6 1 4 1.0689431

# 5 5.0 1 5 1.0792841

# 6 5.4 1 6 0.9182002

#1.1 横向云雨图绘制:

p1 <- raincloud_1x1(

data = dt1,

colors = (c('#0da9ce','#e74a32')), #描边颜色

fills = (c('#0da9ce','#e74a32')), #填充颜色

size = 1.2, #散点大小

alpha = 0.5, #不透明度

ort = 'h' #横向,v则纵向云雨图

)

scale_x_continuous(breaks = c(1.4, 2.4),#间距

labels = c("setosa", "versicolor"),#标签

limits = c(0.8, 3)) #通过限制/修改坐标轴来更改云雨图位置[两个分组的x轴坐标在绘图数据中为1和2]

labs(x = "Species", y = "Sepal.Length")

theme_classic()

p1

#1.2 纵向云雨图绘制:在上面的基础上进行了翻转操作

p2 <- raincloud_1x1(

data = dt1,

colors = (c('#0da9ce','#e74a32')),

fills = (c('#0da9ce','#e74a32')),

size = 1.2,

alpha = 0.5,

ort = 'v' #翻转

)

scale_x_continuous(breaks = c(1.4, 2.4),

labels = c("setosa", "versicolor"),#x轴标签

limits = c(0.8, 3))

labs(x = "Species", y = "Sepal.Length")

theme_classic()

p2

#1.3 配对云雨图绘制:

head(dt1) #对两组中的相同id进行配对;

p3 <- raincloud_1x1_repmes(

data = dt1,

colors = (c('#0da9ce','#e74a32')),

fills = (c('#0da9ce','#e74a32')),

size = 1.6,

alpha = 0.5,

line_color = 'grey', #配对连线颜色

line_alpha = 0.8, #配对连线不透明度

align_clouds = FALSE

# align_clouds = T#是否对齐云朵[默认F为展开,若为T则将云朵对齐放在一侧]

)

scale_x_continuous(breaks = c(0.6, 2.4),

labels = c("setosa", "versicolor"),

limits = c(0, 3))

labs(x = "Species", y = "Sepal.Length")

theme_classic()

p3

# #对齐云朵:

# p4 <- raincloud_1x1_repmes(

# data = dt1,

# colors = (c('#0da9ce','#e74a32')),

# fills = (c('#0da9ce','#e74a32')),

# size = 1.6,

# alpha = 0.5,

# line_color = 'grey',

# line_alpha = 0.8,

# align_clouds = TRUE #云朵放在一侧

# )

# scale_x_continuous(breaks = c(1, 2.4),

# labels = c("setosa", "versicolor"),

# limits = c(0.5, 3))

# labs(x = "Species", y = "Sepal.Length")

# theme_classic()

# p4

#2.1 双刻度重复配对云雨图绘制

#绘图数据整理:

dt2 <- data_2x2(

array_1 = iris$Sepal.Length[1:50],

array_2 = iris$Sepal.Length[51:100],

array_3 = iris$Sepal.Length[101:150],

array_4 = iris$Sepal.Length[81:130], #共设定4个数组

labels = (c('control','test')), #两个组标签的连接字符串

jit_distance = 0.09,

jit_seed = 123,

spread_x_ticks = FALSE) # 2个x_axis为 FALSE,如果4个x_axis则为TRUE

head(dt2)

p5 <- raincloud_2x2_repmes(

data = dt2,

colors = (c('#0273c2', '#efc001', '#0273c2', '#efc001')),

fills = (c('#0273c2', '#efc001', '#0273c2', '#efc001')),

size = 1.5,

alpha = 0.6,

spread_x_ticks = FALSE

)

scale_x_continuous(breaks = c(0.5, 2.5),

labels = c("day1", "day10"),

limits = c(0, 3))

labs(x = "Time", y = "Length")

theme_classic()

p5

#2.2 四刻度重复配对云雨图绘制

#绘图数据整理:

dt3 <- data_2x2(

array_1 = iris$Sepal.Length[1:50],

array_2 = iris$Sepal.Length[51:100],

array_3 = iris$Sepal.Length[101:150],

array_4 = iris$Sepal.Length[81:130],

labels = (c('control','test')),

jit_distance = 0.09,

jit_seed = 123,

spread_x_ticks = TRUE) #4刻度(x_axis),设为T

tail(dt3)

p6 <- raincloud_2x2_repmes(

data = dt3,

colors = (c('#0273c2', '#efc001', '#0273c2', '#efc001')),

fills = (c('#0273c2', '#efc001', '#0273c2', '#efc001')),

size = 1.5,

alpha = 0.6,

spread_x_ticks = TRUE #4刻度,设为T

)

scale_x_continuous(breaks = c(1,2,3,4),

labels = c("day1", "day10", "day1", "day10"),

limits = c(0, 5))

labs(x = "Time", y = "Length")

theme_classic()

p6

#数据整理:

dt4 <- data_2x2(

array_1 = iris$Sepal.Length[1:50],

array_2 = iris$Sepal.Length[51:100],

array_3 = iris$Sepal.Length[101:150],

array_4 = iris$Sepal.Length[81:130],

array_5 = iris$Sepal.Length[21:70],

array_6 = iris$Sepal.Length[41:90],

labels = (c('control','test')),

jit_distance = 0.05,

jit_seed = 123)

tail(dt4)

##横向云雨图

p7 <- raincloud_2x3_repmes(

data = dt4,

colors = (c('#137b6a', '#a3bfe6', '#137b6a', '#a3bfe6', '#137b6a', '#a3bfe6')),

fills = (c('#137b6a', '#a3bfe6', '#137b6a', '#a3bfe6', '#137b6a', '#a3bfe6')),

size = 1.5,

alpha = 0.6,

ort = 'h'

)

scale_x_continuous(breaks=c(1,2,3),

labels=c("D1", "D2", "D3"),

limits=c(0.7, 4))

labs(x = "Time", y = "Score")

theme_classic()

p7

##可以用于绘画单细胞的数据基因对应细胞的表现

# 注意数据格式的转化

0 人点赞