ggplot2绘制小提琴图

本文记录小提琴图绘图模板。

目录

0. 导入数据

1. 无数据点小提琴图

2. 有数据点小提琴图

3. 分组小提琴

4. 半小提琴

5. 半小提琴图联合散点图、箱线图复杂图形 

6. 云雨图


0. 导入数据
#加载包
library(ggplot2) # Create Elegant Data Visualisations Using the Grammar of Graphics
library(palmerpenguins) # Palmer Archipelago (Antarctica) Penguin Data
library(tidyverse) # Easily Install and Load the 'Tidyverse'
library(ggforce) # Accelerating 'ggplot2'
library(gghalves) # Compose Half-Half Plots Using Your Favourite Geoms
library(ggdist) # Visualizations of Distributions and Uncertainty

#使用palmerpenguin包中的数据
df <- penguins
# df <- read.table("data.txt", header = 1, check.names = F, sep = "\t")
#使用tidyverse包对数据进行处理
df <- df %>% 
  drop_na()
1. 无数据点小提琴图
p1 <- ggplot(df, aes(x=species, y=flipper_length_mm, fill=species))+#指定数据
  geom_violin()+
  scale_fill_manual(values = c("#5cc3e8","#ffdb00","#79ceb8"))+
  theme_bw()+
  theme(panel.grid = element_blank(),
        axis.text = element_text(color = 'black',size=12),
        axis.title = element_text(color = 'black',size=15),
        legend.position = "none")
p1

2. 有数据点小提琴图

3. 分组小提琴
p3 <- ggplot(df, aes(x=species, y=flipper_length_mm, fill=sex))+#指定数据
  geom_violin()+
  geom_sina(alpha=0.5,size=2,color="black")+
  scale_fill_manual(values = c("#5cc3e8","#ffdb00"))+
  theme_bw()+
  theme(panel.grid = element_blank(),
        axis.text = element_text(color = 'black',size=12),
        axis.title = element_text(color = 'black',size=15),
        legend.title = element_text(color = 'red',size=15),
        legend.text = element_text(color = 'black',size=10))
p3

4. 半小提琴
p4 <- ggplot(df, aes(x=species, y=flipper_length_mm, fill=species))+#指定数据
  geom_half_violin()+
  scale_fill_manual(values = c("#5cc3e8","#ffdb00","#79ceb8"))+
  theme_bw()+
  theme(panel.grid = element_blank(),
        axis.text = element_text(color = 'black',size=12),
        axis.title = element_text(color = 'black',size=15),
        legend.position = "none")
p4

5. 半小提琴图联合散点图、箱线图复杂图形 
p5 <- ggplot(df,aes(species,flipper_length_mm,fill=species))+
  geom_half_violin(position = position_nudge(x=0.25),side = "r",width=0.8,color=NA)+
  geom_boxplot(width=0.4,size=1.2,outlier.color =NA)+
  geom_jitter(aes(fill=species),shape=21,size=2.5,width=0.2)+
  scale_fill_manual(values = c("#5cc3e8","#ffdb00","#79ceb8"))+
  theme_bw()+
  theme(panel.grid = element_blank(),
        axis.text = element_text(color = 'black',size=12),
        axis.title = element_text(color = 'black',size=15),
        legend.position = "none")
p5

6. 云雨图
p6 <- ggplot(df,aes(species,flipper_length_mm,fill=species))+
  stat_slab(aes(thickness = after_stat(pdf * n)),scale = 0.7)+#绘制半小提琴图
  stat_dotsinterval(side = "bottom", 
                    scale = 0.7, 
                    slab_size = NA)+#通过添加“stat_dotsinterval”功能,可以制作雨云图
  coord_flip()+
  scale_fill_manual(values = c("#5cc3e8","#ffdb00","#79ceb8"))+
  theme_bw()+
  theme(panel.grid = element_blank(),
        axis.text = element_text(color = 'black',size=12),
        axis.title = element_text(color = 'black',size=15),
        legend.position = "none")
p6

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qqqqqrc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值