image
image
參考
代碼來源Split Violin Plot for ggplot2 · GitHub
關注我們的公眾號,可見詳細復現Fancy violin (qq.com)
實戰
我首先找到一個合適的顏色,并且定義了自己的作圖主題
library(ggplot2)
library(ggsci)
library(ggpubr)
library(scales)
###自定義顏色
mypal=pal_simpsons(alpha = .6)(9)
mypal[c(1,7)]
show_col(mypal)
show_col(mypal[c(1,7)])
###自定義主題
mytheme <- theme(axis.text.x=element_text(size=12),
axis.text.y=element_text(size=12),
axis.title=element_text(size = 13),
legend.text=element_text(size=12),
legend.title=element_text(size=12),
axis.line = element_line(size=0.7),
panel.border = element_blank(),
panel.grid = element_blank())
方法一
image
###數據生成
cell <- rep(LETTERS[1:10],400)
sp <- rep(c("normal","tumor"),time=c(2000,2000))
value <- c(rnorm(2000)+1,rnorm(2000)+2)
df <- data.frame(cell=cell,sample=sp,value=value)
> head(df)
cell sample value
1 A normal 0.3682008
2 B normal 1.4030093
3 C normal -0.8951433
4 D normal 1.2949975
5 E normal 2.0451915
6 F normal 1.6834365
###作圖
ggplot(df,aes(x=cell,y = value,fill=sample))+
geom_split_violin(trim = T,colour=NA)+
geom_point(stat = 'summary',fun=mean,
position = position_dodge(width = 0.9))+
scale_fill_manual(values = c("#197EC099","#FED43999"))+
stat_summary(fun.min = function(x){quantile(x)[2]},
fun.max = function(x){quantile(x)[4]},
geom = 'errorbar',color='black',
width=0.01,size=0.5,
position = position_dodge(width = 0.9))+
theme_bw()+
mytheme+
ylab("Value")+xlab("Type")
方法二
image
ggplot()+
geom_half_violin(
data = df %>% filter(sample=="normal"),
aes(x = cell,y = value),colour=NA,fill="#197EC099",side = "l"
)+
geom_half_violin(
data = df %>% filter(sample=="tumor"),
aes(x = cell,y = value),colour=NA,fill="#FED43999",side = "r"
)+
mytheme+
theme_bw()+
ylab("Value")+xlab("Type")+
geom_point(data = df,aes(x=cell,y = value,fill=sample),
stat = 'summary',fun=mean,
position = position_dodge(width = 0.7))+
stat_summary(data = df,aes(x=cell,y = value,fill=sample),
fun.min = function(x){quantile(x)[2]},
fun.max = function(x){quantile(x)[4]},
geom = 'errorbar',color='black',
width=0.01,size=0.5,
position = position_dodge(width = 0.7))+
stat_compare_means(data = df,aes(x=cell,y = value,fill=sample),label = 'p.signif')
總的來說,兩種方法得到的結果一樣,方法一依賴包裝好的函數,方法二較為成熟,可以直接使用gghalves包。
GGHALVES包
描述
這里主要介紹這個包中的geom_half_violin()
函數,它相當于geom_violin()
函數的變體,因為這個函數主要作用就是展示一半的小提琴圖,然后與其他圖形組合。
使用
geom_half_violin(mapping = NULL, data = NULL, stat = "half_ydensity",
position = "dodge", ..., side = "l", nudge = 0,
draw_quantiles = NULL, trim = TRUE, scale = "area",
na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
stat_half_ydensity(mapping = NULL, data = NULL, geom = "half_violin",
position = "dodge", ..., bw = "nrd0", adjust = 1,
kernel = "gaussian", trim = TRUE, scale = "area", na.rm = FALSE,
show.legend = NA, inherit.aes = TRUE)
參數
參數 | 解釋 |
---|---|
mapping |
通過aes() 指定圖形屬性映射。默認為NULL ,使用ggplot() 中aes() 指定的映射。 |
data |
指定數據框。默認為NULL ,使用ggplot() 中的數據。 |
stat |
覆蓋geom_density()和stat_density()之間的默認連接。 |
position |
位置調整,可以是字符串,默認為"dodge" ,也可以是位置調整函數的調用結果。 |
... |
其他參數,通常是圖形屬性,將其設置為固定值,如color = "red" 或者size = 3 。 |
side |
畫半小提琴圖的一側。 “ l”代表左,“ r”代表右,默認為“ l”。 |
nudge |
在小提琴圖和分配給x軸上給定因子的空間中間之間添加空間。 |
draw_quantiles |
如果不是MULL (默認為NULL ),在給定的密度估計分位數處繪制水平線。 |
trim |
若為TRUE (默認),將小提琴的尾部修整到數據范圍。 若為FALSE ,不修剪尾巴。 |
scale |
如果為"area" (默認),則所有小提琴都具有相同的面積(修剪尾部之前)。 |
如果為"count" ,則面積將與觀察值成比例地縮放。如果為"width" ,則所有小提琴都具有相同的最大寬度。 | |
na.rm |
如果為FALSE (默認),則會使用警告刪除缺失值。如果為TRUE ,則會自動刪除缺少的值。 |
show.legend |
邏輯值,默認為NA ,若為FALSE ,不顯示該圖層的圖例;若為TRUE ,則顯示該圖層的圖例。它也可以是帶有名稱(圖形屬性)的邏輯向量,用來選擇要顯示的圖形屬性。如show.legend = c(size = TRUE,color = FALSE) 表示顯示size 對應的圖例,而不顯示color 對應的圖例。 |
inherit.aes |
默認為TRUE ,若為FALSE ,覆蓋ggplot() 中aes() 默認屬性,而不是與他們組合。 |
geom |
覆蓋geom_density() 和stat_density() 之間的默認連接。 |
bw |
要使用的平滑帶寬度。如果是數字,則為平滑內核的標準差。 |
adjust |
多次帶寬調整。這使得可以在仍使用帶寬估計器的情況下調整帶寬。例如,adjust = 1/2 表示使用默認帶寬的一半。 |
kernel |
內核,平滑曲線方法。詳見:density() |
云雨圖
library(tidyverse)
# 統計摘要
summ_iris <- iris %>%
group_by(Species) %>%
summarise(
mean = mean(Sepal.Length),
sd = sd(Sepal.Length),
n = n()
) %>%
mutate(se = sd/sqrt(n),
Species = factor(Species, levels = c('versicolor', 'setosa', 'virginica')))
# 數據轉換
iris_plot <- iris %>%
mutate(Species = factor(Species, levels = c('versicolor', 'setosa', 'virginica')))
# 繪圖
library(gghalves)
library(ggpubr)
library(ggsignif)
ggplot(iris_plot , aes(x = Species, y = Sepal.Length, fill = Species))+
geom_half_violin(aes(fill = Species),
position = position_nudge(x = .15, y = 0),
adjust=1.5, trim=FALSE, colour=NA, side = 'r') +
geom_point(aes(x = as.numeric(Species) - 0.1,
y = Sepal.Length,color = Species),
position = position_jitter(width = .05),size = .25, shape = 20) +
geom_boxplot(aes(x = Species,y = Sepal.Length, fill = Species),
outlier.shape = NA,
width = .05,
color = "black")+
geom_point(data=summ_iris,
aes(x=Species,y = mean,group = Species, color = Species),
shape=18,
size = 1.5,
position = position_nudge(x = .1,y = 0)) +
geom_errorbar(data = summ_iris,
aes(x = Species, y = mean, group = Species, colour = Species,
ymin = mean-se, ymax = mean+se),
width=.05,
position=position_nudge(x = .1, y = 0)
) +
scale_color_jco() +
scale_fill_jco() +
geom_signif(comparisons = list(c("versicolor", "setosa"),
c("versicolor", "virginica"),
c("setosa", "virginica")),
y_position = c(8.2, 8.6, 8.4),
map_signif_level = c("***" = 0.001, "**" = 0.01, "*" = 0.05)) +
ggsave('云雨圖.pdf', width = 7, height = 6)
image.png
轉載來自:
作者:芋圓學徒
鏈接:http://www.lxweimin.com/p/679b7f9c5c27
作者:weixin_43700050
鏈接:https://blog.csdn.net/weixin_43700050/article/details/107512448
相關文章:
gghalves包-你五毛我五毛
ggplot2實現分半小提琴圖繪制基因表達譜和免疫得分
不同方法畫half -小提琴圖
R語言作圖——Split violin plot