小提琴图的美化--对半小提琴+箱式
效果图
代码
####----小提琴图数据准备----#####
gene <- c("CD3D","CSTB","APOE","EGFR","VEGFA","IL6")
exprs <- data.frame(FetchData(object = c6, vars = c("Anno.chi",gene,"group")) )
exprs$Proj <- "Seurat"
# exprs$group <- factor(exprs$group,levels=c('Control',''))
########----对半小提琴图-----#############
library(Seurat)
library(tidyverse)
#devtools::install_github('erocoar/gghalves')
library(gghalves)
library(gridExtra)
png(paste0( "Vlnplot-c6_IL6-美化1-半小提琴.png"), width = 4, height = 4, units = "in", res = 400)
p <- ggplot() +
geom_half_violin(data = exprs[exprs$group == 'Control',],
aes(x = Proj, y = IL6, fill = group),
color = 'black',
scale = 'width')
#在上图基础上叠加右边,绘图逻辑相同
p1 <- p +
geom_half_violin(data = exprs[exprs$group == 'Suicide',],
aes(x = Proj, y = IL6, fill = group),
color = 'black',
scale = 'width',
side = 'r')+
theme_bw() +
theme(
panel.grid = element_blank()) +
scale_fill_manual(values = c("#E39A35","#68A180")) +
labs(x = "", y = 'Expression Level',title="IL6")
print(p1)
dev.off()
###################
library(ggpubr) #用于统计分析添加统计指标
png(paste0( "Vlnplot-c6_IL6-美化2-箱式图.png"), width = 4, height = 4, units = "in", res = 400)
p <- ggviolin(exprs, x = "group", # 分组列
y = "IL6", # 基因列
fill = "group", #按分组填充颜色
alpha = 1,#透明图 0-1
width = 0.5, #宽度
legend = "top",legend.title = "IL6",#legend及位置
font.legend = c(12, "bold", "black"),
ylim=c(3, 6), #y轴长度#####(根据具体情况可更改)
ylab="Normalized Expression", xlab=FALSE, #xy轴标签,去掉X轴标签
font.y = 16,#xy轴标题大小
x.text.angle = 45, y.text.angle = 90,#xy轴标题角度
font.tickslab = c(15,"plain","black"), #xy轴刻度大小/样式/颜色
add = "boxplot", #添加图 "dotplot", "jitter", "boxplot", "point"
add.params = list(fill = "white", #填充颜色 白色
width = 0.1,#宽度
linetype = 1)#线型
)
print(p)
dev.off()
原始学习链接:
https://mp.weixin.qq.com/s/K_56BmYLw9GazBk541r6DA
https://mp.weixin.qq.com/s/AhkW9Rm01LaRYiNj_dn7-A